Skip to content

Commit

Permalink
support elem set and elem get
Browse files Browse the repository at this point in the history
  • Loading branch information
yviansu committed Oct 16, 2023
1 parent fcc84c6 commit 935d304
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/builtin/builtin_name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export namespace BuiltinNames {
export const tableInitialPages = 1;
export const tableMaximumPages = 10;
export const tableGrowDelta = 10;
export const memoryReserveOffset = 0;
export const memoryReserveMaxSize = 100;

// wasm function
export const start = '~start';
Expand Down
132 changes: 132 additions & 0 deletions src/backend/binaryen/wasm_expr_gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
arrayToPtr,
emptyStructType,
generateArrayStructTypeInfo,
StringRefMeatureOp,
} from './glue/transform.js';
import { assert } from 'console';
import { WASMGen } from './index.js';
Expand Down Expand Up @@ -3528,6 +3529,71 @@ export class WASMExpressionGen {
stringTypeInfo.typeRef,
);
}
case ValueTypeKind.OBJECT: {
const valueType = value.type;
const indexStrRef = this.wasmExprGen(value.index);
/* measure str length */
const propStrLen = binaryenCAPI._BinaryenStringMeasure(
this.module.ptr,
StringRefMeatureOp.UTF8,
indexStrRef,
);
const storeInMemoryStmts: binaryen.ExpressionRef[] = [];
/* encode str to memory */
const codeunits = binaryenCAPI._BinaryenStringEncode(
this.module.ptr,
StringRefMeatureOp.WTF8,
indexStrRef,
this.module.i32.const(BuiltinNames.memoryReserveOffset),
0,
);
storeInMemoryStmts.push(codeunits);
/* add end to memory */
storeInMemoryStmts.push(
this.module.i32.store(
0,
4,
this.module.i32.add(
this.module.i32.const(
BuiltinNames.memoryReserveOffset,
),
codeunits,
),
this.module.i32.const(0),
),
);
const metaRef = getWASMObjectMeta(this.module, ownerRef);
/* invoke set_indirect to set prop value to obj */
const flag = ItableFlag.FIELD;
const indexRef = this.getPropIndexOfInfc(
metaRef,
this.module.i32.const(BuiltinNames.memoryReserveOffset),
flag,
);
const setOp = this.dynGetInfcField(
ownerRef,
indexRef,
valueType,
false,
this.getPropTypeOnIndexOfInfc(
metaRef,
this.module.i32.const(BuiltinNames.memoryReserveOffset),
flag,
),
);
storeInMemoryStmts.push(setOp);

return this.module.if(
this.module.i32.ge_s(
propStrLen,
this.module.i32.const(
BuiltinNames.memoryReserveMaxSize,
),
),
binaryen.unreachable,
this.module.block(null, storeInMemoryStmts),
);
}
default:
throw Error(`wasmIdxGet: ${value}`);
}
Expand Down Expand Up @@ -3569,6 +3635,72 @@ export class WASMExpressionGen {
targetValueRef,
);
}
case ValueTypeKind.OBJECT: {
const valueType = value.value!.type;
const indexStrRef = this.wasmExprGen(value.index);
/* measure str length */
const propStrLen = binaryenCAPI._BinaryenStringMeasure(
this.module.ptr,
StringRefMeatureOp.UTF8,
indexStrRef,
);
const storeInMemoryStmts: binaryen.ExpressionRef[] = [];
/* encode str to memory */
const codeunits = binaryenCAPI._BinaryenStringEncode(
this.module.ptr,
StringRefMeatureOp.WTF8,
indexStrRef,
this.module.i32.const(BuiltinNames.memoryReserveOffset),
0,
);
storeInMemoryStmts.push(codeunits);
/* add end to memory */
storeInMemoryStmts.push(
this.module.i32.store(
0,
4,
this.module.i32.add(
this.module.i32.const(
BuiltinNames.memoryReserveOffset,
),
codeunits,
),
this.module.i32.const(0),
),
);
const metaRef = getWASMObjectMeta(this.module, ownerRef);
/* invoke set_indirect to set prop value to obj */
const flag = ItableFlag.FIELD;
const indexRef = this.getPropIndexOfInfc(
metaRef,
this.module.i32.const(BuiltinNames.memoryReserveOffset),
flag,
);
const setOp = this.dynSetInfcField(
ownerRef,
indexRef,
valueType,
false,
this.getPropTypeOnIndexOfInfc(
metaRef,
this.module.i32.const(BuiltinNames.memoryReserveOffset),
flag,
),
this.wasmExprGen(value.value!),
);
storeInMemoryStmts.push(setOp);

return this.module.if(
this.module.i32.ge_s(
propStrLen,
this.module.i32.const(
BuiltinNames.memoryReserveMaxSize,
),
),
binaryen.unreachable,
this.module.block(null, storeInMemoryStmts),
);
}
default:
throw Error(`wasmIdxSet: ${value}`);
}
Expand Down

0 comments on commit 935d304

Please sign in to comment.