Skip to content

Commit

Permalink
cleanup: copy var data out of memory into Uint8Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Jun 19, 2024
1 parent 3efa804 commit 0f50f0f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/call-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class CallContext {
#encoder: TextEncoder;
#arrayBufferType: { new (size: number): ArrayBufferLike };
#config: PluginConfig;
#vars: Map<string, PluginOutput> = new Map();
#vars: Map<string, Uint8Array> = new Map();

/** @hidden */
constructor(type: { new (size: number): ArrayBufferLike }, logger: Console, config: PluginConfig) {
Expand Down Expand Up @@ -84,7 +84,7 @@ export class CallContext {
*
* @returns {@link PluginOutput}
*/
getVariable(name: string): PluginOutput | null {
getVariable(name: string): Uint8Array | null {
if (!this.#vars.has(name)) {
return null;
}
Expand All @@ -100,7 +100,9 @@ export class CallContext {
if (typeof value === 'string'){
value = this.#encoder.encode(value);
}
this.#vars.set(name, new PluginOutput(value.buffer));
const buf = new Uint8Array(value.buffer.byteLength);
buf.set(new Uint8Array(value.buffer));
this.#vars.set(name, buf);
}

/**
Expand Down Expand Up @@ -252,7 +254,7 @@ export class CallContext {

const key = item.string();
if (this.#vars.has(key)){
const value = this.store(this.#vars.get(key)!.bytes());
const value = this.store(this.#vars.get(key)!);
return value;
}

Expand All @@ -274,7 +276,9 @@ export class CallContext {

const value = this.read(valueaddr);
if (value){
this.#vars.set(key, value);
const buf = new Uint8Array(value!.buffer.byteLength);
buf.set(value.bytes());
this.#vars.set(key, buf);
}
},

Expand Down

0 comments on commit 0f50f0f

Please sign in to comment.