feat(js): add direct Blob loading

This commit is contained in:
Kirill Zhumarin 2026-08-29 20:10:13 +03:00
parent f087b21417
commit 96c1c91b5d
6 changed files with 115 additions and 54 deletions

View file

@ -16,6 +16,9 @@
files, following the established project structure.
- Use tabs for indentation and preserve the surrounding brace and spacing
conventions.
- Always use braces for multiline `if`, `for`, and `while` bodies, even when
the body contains a single statement. Braces may be omitted only when the
complete body fits on one short line.
- In a multiline ternary expression, keep `?` and `:` at the ends of their
respective lines:

21
JS.md
View file

@ -38,7 +38,6 @@ type Ptr89Arch = "arm" | "c166";
interface Ptr89OpenOptions {
arch?: Ptr89Arch;
base?: number;
align?: number;
}
open(data: Uint8Array, options?: Ptr89OpenOptions): Promise<void>;
@ -52,18 +51,26 @@ extends `Uint8Array`.
- `arch` selects the instruction decoder. Default: `"arm"`.
- `base` sets the load address. The ARM default is `0xA0000000`. For C166 it is
calculated as `0x1000000 - data.byteLength`.
- `align` sets the search alignment in bytes. Default: `1`.
### `find(pattern, limit?)`
### `openFile(file, options?)`
```ts
find(pattern: string, limit?: number): Ptr89SearchResult[];
openFile(file: Blob, options?: Ptr89OpenOptions): Promise<void>;
```
Reads a `File` or `Blob` directly into WebAssembly memory. Options are the same
as for `open()`.
### `find(pattern, limit?, align?)`
```ts
find(pattern: string, limit?: number, align?: number): Ptr89SearchResult[];
```
Finds one pattern in the opened fullflash. The default limit is `100`; a limit
of `0` disables it. `address` is the resolved pattern result, while `offset`
and `bytes` describe its location in the fullflash. Fixed addresses contain
only `address`:
of `0` disables it. The default alignment is `1`. `address` is the resolved
pattern result, while `offset` and `bytes` describe its location in the
fullflash. Fixed addresses contain only `address`:
```ts
interface Ptr89SearchResult {

View file

@ -9,7 +9,6 @@ export type Ptr89XRefType = "pointer" | "reference" | "branch";
export interface Ptr89OpenOptions {
arch?: Ptr89Arch;
base?: number;
align?: number;
}
export interface Ptr89SearchResult {
@ -55,44 +54,39 @@ function validateLimit(limit: number): void {
throw new RangeError("Result limit must be a non-negative integer.");
}
function validateAlign(align: number): void {
if (!Number.isSafeInteger(align) || align <= 0)
throw new RangeError("Search alignment must be a positive integer.");
}
export class Ptr89 {
private module?: WasmModule;
private handle?: WasmPtr89;
async open(data: Uint8Array, options: Ptr89OpenOptions = {}): Promise<void> {
const arch = options.arch ?? "arm";
const align = options.align ?? 1;
if (!Number.isSafeInteger(align) || align <= 0)
throw new RangeError("Memory alignment must be a positive integer.");
await this.openData(data.byteLength, options, (module, ptr) => module.HEAPU8.set(data, ptr));
}
let base = options.base;
if (base === undefined) {
if (arch === "c166") {
if (data.byteLength > C166_ADDRESS_SPACE_SIZE)
throw new RangeError("C166 fullflash is larger than the 16 MiB address space; specify base explicitly.");
base = C166_ADDRESS_SPACE_SIZE - data.byteLength;
} else {
base = 0xA0000000;
async openFile(file: Blob, options: Ptr89OpenOptions = {}): Promise<void> {
await this.openData(file.size, options, async (module, ptr) => {
const reader = file.stream().getReader();
let offset = 0;
try {
while (true) {
const { done, value } = await reader.read();
if (done)
break;
if (value.byteLength > file.size - offset)
throw new RangeError("Blob stream is larger than the declared size.");
module.HEAPU8.set(value, ptr + offset);
offset += value.byteLength;
}
} finally {
reader.releaseLock();
}
}
if (!Number.isSafeInteger(base) || base < 0 || base > 0xFFFFFFFF)
throw new RangeError("Memory base must be a 32-bit unsigned integer.");
const module = await loadModule();
const handle = this.handle ?? new module.Ptr89();
const ptr = module._malloc(data.byteLength);
module.HEAPU8.set(data, ptr);
try {
handle.open(ptr, data.byteLength, base, align, arch);
this.module = module;
this.handle = handle;
} catch (error) {
if (!this.handle)
handle.delete();
throw getError(module, error);
} finally {
module._free(ptr);
}
if (offset !== file.size)
throw new RangeError("Blob stream is smaller than the declared size.");
});
}
close(): void {
@ -108,11 +102,12 @@ export class Ptr89 {
this.getHandle().setDebug(enabled);
}
find(pattern: string, limit = 100): Ptr89SearchResult[] {
find(pattern: string, limit = 100, align = 1): Ptr89SearchResult[] {
validateLimit(limit);
validateAlign(align);
const [module, handle] = this.getState();
try {
const matches = handle.find(pattern, limit);
const matches = handle.find(pattern, limit, align);
try {
return Array.from({ length: matches.size() }, (_, i) => {
const match = matches.get(i);
@ -162,6 +157,40 @@ export class Ptr89 {
}
}
private async openData(size: number, options: Ptr89OpenOptions,
write: (module: WasmModule, ptr: number) => void | Promise<void>): Promise<void> {
const arch = options.arch ?? "arm";
let base = options.base;
if (base === undefined) {
if (arch === "c166") {
if (size > C166_ADDRESS_SPACE_SIZE)
throw new RangeError("C166 fullflash is larger than the 16 MiB address space; specify base explicitly.");
base = C166_ADDRESS_SPACE_SIZE - size;
} else {
base = 0xA0000000;
}
}
if (!Number.isSafeInteger(base) || base < 0 || base > 0xFFFFFFFF)
throw new RangeError("Memory base must be a 32-bit unsigned integer.");
const module = await loadModule();
const handle = this.handle ?? new module.Ptr89();
const ptr = module._malloc(size);
try {
await write(module, ptr);
handle.open(ptr, size, base, arch);
this.module = module;
this.handle = handle;
} catch (error) {
if (!this.handle)
handle.delete();
throw getError(module, error);
} finally {
module._free(ptr);
}
}
private getHandle(): WasmPtr89 {
if (!this.handle)
throw new Error("Ptr89 is not opened.");

View file

@ -41,6 +41,7 @@ describe("Ptr89 WASM", () => {
offset: 10,
bytes: "10B5",
}]);
expect(ptr89.find("48", 1, 2)).toEqual([]);
ptr89.close();
});
@ -71,6 +72,28 @@ describe("Ptr89 WASM", () => {
ptr89.close();
});
it("opens a Blob directly", async () => {
const memory = Uint8Array.from([
0x00, 0xBF, // NOP
0x10, 0xB5, // PUSH {R4, LR}
]);
const blob = new Blob([memory.subarray(0, 2), memory.subarray(2)]);
blob.arrayBuffer = () => {
throw new Error("arrayBuffer() must not be used");
};
const ptr89 = new Ptr89();
await ptr89.openFile(blob, { arch: "arm" });
expect(ptr89.find("10 B5", 1)).toEqual([{
address: 0xA0000003,
offset: 2,
bytes: "10B5",
}]);
ptr89.close();
});
it("prettifies patterns", async () => {
await expect(prettify("00bf")).resolves.toBe("00 BF");
await expect(prettify("&BL(00bf) ")).resolves.toBe("&BL(00 BF)");

View file

@ -22,11 +22,9 @@ Ptr89Wasm::Ptr89Wasm() {
spdlog::set_level(spdlog::level::warn);
}
void Ptr89Wasm::open(uintptr_t ptr, size_t size, uint32_t base, int align, const std::string &archName) {
void Ptr89Wasm::open(uintptr_t ptr, size_t size, uint32_t base, const std::string &archName) {
if (ptr == 0 && size != 0)
throw std::invalid_argument("Memory pointer is null.");
if (align <= 0)
throw std::invalid_argument("Memory alignment must be greater than zero.");
Architecture arch;
if (archName == "arm") {
@ -42,7 +40,6 @@ void Ptr89Wasm::open(uintptr_t ptr, size_t size, uint32_t base, int align, const
if (size != 0)
m_data.assign(data, data + size);
m_base = base;
m_align = align;
m_arch = arch;
m_open = true;
}
@ -57,8 +54,8 @@ void Ptr89Wasm::setDebug(bool enabled) {
spdlog::set_level(enabled ? spdlog::level::debug : spdlog::level::warn);
}
std::vector<Ptr89SearchResult> Ptr89Wasm::find(const std::string &patternText, size_t limit) const {
auto memory = getMemory();
std::vector<Ptr89SearchResult> Ptr89Wasm::find(const std::string &patternText, size_t limit, int align) const {
auto memory = getMemory(align);
auto pattern = Pattern::parse(patternText);
auto matches = Pattern::find(pattern, memory, limit);
@ -74,16 +71,19 @@ std::vector<Ptr89XRef> Ptr89Wasm::findXRefs(uint32_t address, size_t limit) cons
auto matches = Pattern::finXRefs(address, memory, limit);
std::vector<Ptr89XRef> results;
results.reserve(matches.size());
for (const auto &match: matches)
for (const auto &match: matches) {
results.push_back({ Pattern::getResultTypeName(match.type), match.address, match.offset,
getBytes(match.offset, match.size, memory) });
}
return results;
}
Pattern::Memory Ptr89Wasm::getMemory() const {
Pattern::Memory Ptr89Wasm::getMemory(int align) const {
if (!m_open)
throw std::runtime_error("Ptr89 is not opened.");
return { m_base, m_data.data(), m_data.size(), m_align, m_arch };
if (align <= 0)
throw std::invalid_argument("Search alignment must be greater than zero.");
return { m_base, m_data.data(), m_data.size(), align, m_arch };
}
std::string prettify(const std::string &pattern) {

View file

@ -26,19 +26,18 @@ class Ptr89Wasm {
private:
std::vector<uint8_t> m_data;
uint32_t m_base = 0;
int m_align = 1;
Architecture m_arch = ARCH_ARM;
bool m_open = false;
Pattern::Memory getMemory() const;
Pattern::Memory getMemory(int align = 1) const;
public:
Ptr89Wasm();
void open(uintptr_t ptr, size_t size, uint32_t base, int align, const std::string &archName);
void open(uintptr_t ptr, size_t size, uint32_t base, const std::string &archName);
void close();
void setDebug(bool enabled);
std::vector<Ptr89SearchResult> find(const std::string &patternText, size_t limit) const;
std::vector<Ptr89SearchResult> find(const std::string &patternText, size_t limit, int align) const;
std::vector<Ptr89XRef> findXRefs(uint32_t address, size_t limit) const;
};