Skip to content

Commit

Permalink
change object property get and object property set (#40)
Browse files Browse the repository at this point in the history
* change object property get and object property set

Signed-off-by: Su Yihan <[email protected]>

---------

Signed-off-by: Su Yihan <[email protected]>
  • Loading branch information
yviansu authored Oct 30, 2023
1 parent 23821c5 commit 833babd
Show file tree
Hide file tree
Showing 14 changed files with 946 additions and 711 deletions.
3 changes: 3 additions & 0 deletions lib/builtin/builtin_name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export namespace BuiltinNames {

// builtin function name
export const globalInitFuncName = 'global|init|func';
export const findPropertyFlagAndIndex = 'find_property_flag_and_index';
export const findPropertyType = 'find_property_type';
export const getInfcProperty = 'get_infc_property';

// builtin globals
export const builtinTypeManglePrefix = 'lib/builtin/lib.type.d';
Expand Down
35 changes: 16 additions & 19 deletions runtime-library/utils/type_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1012,44 +1012,41 @@ get_prop_index_of_struct(wasm_exec_env_t exec_env, const char *prop,
uint32_t argc = 3, argv[3] = { 0 }, offset;
wasm_struct_type_t struct_type;
wasm_struct_type_t vtable_type;
int property_flag = -1;
int property_index = -1;

module_inst = wasm_runtime_get_module_inst(exec_env);
wasm_struct_obj = (wasm_struct_obj_t)(*wasm_obj);
wasm_struct_obj_get_field(wasm_struct_obj, 0, false, &vtable_value);
wasm_struct_obj_get_field((wasm_struct_obj_t)vtable_value.gc_obj, 0, false,
&meta);
struct_type = (wasm_struct_type_t)wasm_obj_get_defined_type(*wasm_obj);
func = wasm_runtime_lookup_function(module_inst, "find_index", NULL);
func = wasm_runtime_lookup_function(module_inst,
"find_property_flag_and_index", NULL);
bh_assert(func);

argv[0] = meta.i32;
offset = wasm_runtime_addr_native_to_app(module_inst, (void *)prop);
argv[1] = offset;
argv[2] = 0;
argv[2] = ALL;

wasm_runtime_call_wasm(exec_env, func, argc, argv);
if (argv[0] == -1) {
/* check if flag is method */
argv[0] = meta.i32;
argv[1] = offset;
argv[2] = 1;
wasm_runtime_call_wasm(exec_env, func, argc, argv);
if (argv[0] == -1) {
return -1;
}
else {
if (argv[0] != -1) {
property_flag = argv[0] & META_FLAG_MASK;
property_index = (argv[0] & META_INDEX_MASK) >> 4;
if (property_flag == METHOD) {
vtable_type = (wasm_struct_type_t)wasm_obj_get_defined_type(
vtable_value.gc_obj);
*field_type =
wasm_struct_type_get_field_type(vtable_type, argv[0], &is_mut);
*field_type = wasm_struct_type_get_field_type(
vtable_type, property_index, &is_mut);
}
else if (property_flag == FIELD) {
*field_type = wasm_struct_type_get_field_type(
struct_type, property_index, &is_mut);
}
}
else {
*field_type =
wasm_struct_type_get_field_type(struct_type, argv[0], &is_mut);
}

return argv[0];
return property_index;
}

/**********Utils for search field value of object through meta
Expand Down
1 change: 1 addition & 0 deletions runtime-library/utils/type_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum field_flag {
METHOD = 1,
GETTER = 2,
SETTER = 3,
ALL = 4,
};

typedef enum ts_value_type_t {
Expand Down
13 changes: 5 additions & 8 deletions src/backend/binaryen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ import {
ObjectDescription,
} from '../../semantics/runtime.js';
import { dyntype } from './lib/dyntype/utils.js';
import { clearWasmStringMap, getCString } from './utils.js';
import { assert } from 'console';
import ts from 'typescript';
import { VarValue } from '../../semantics/value.js';
Expand Down Expand Up @@ -97,9 +96,7 @@ export class WASMFunctionContext {
}

i32Local() {
return this.insertTmpVar(
this.binaryenCtx.wasmTypeComp.getWASMType(Primitive.Int),
);
return this.insertTmpVar(binaryen.i32);
}

insert(insn: binaryen.ExpressionRef) {
Expand Down Expand Up @@ -403,7 +400,7 @@ export class WASMGen extends Ts2wasmBackend {
}

private wasmGenerate() {
clearWasmStringMap();
UtilFuncs.clearWasmStringMap();
FunctionalFuncs.resetDynContextRef();

// init wasm environment
Expand Down Expand Up @@ -752,7 +749,7 @@ export class WASMGen extends Ts2wasmBackend {
const heap = this.wasmTypeComp.getWASMHeapType(func.funcType);
funcRef = binaryenCAPI._BinaryenAddFunctionWithHeapType(
this.module.ptr,
getCString(func.name),
UtilFuncs.getCString(func.name),
heap,
arrayToPtr(allVarsTypeRefs).ptr,
allVarsTypeRefs.length,
Expand Down Expand Up @@ -1126,7 +1123,7 @@ export class WASMGen extends Ts2wasmBackend {
);
const expr = binaryenCAPI._BinaryenGlobalSet(
this.module.ptr,
getCString(name),
UtilFuncs.getCString(name),
JSGlobalObj,
);
return expr;
Expand All @@ -1141,7 +1138,7 @@ export class WASMGen extends Ts2wasmBackend {
binaryenCAPI._BinaryenFunctionSetLocalName(
funcRef,
idx,
getCString(name),
UtilFuncs.getCString(name),
);
});
const isBuiltIn = debugFilePath.includes(BuiltinNames.builtinTypeName);
Expand Down
13 changes: 10 additions & 3 deletions src/backend/binaryen/lib/env_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,15 @@ export function addItableFunc(module: binaryen.Module) {
);
const itableLib = fs.readFileSync(itableFilePath, 'utf-8');
const watModule = binaryen.parseText(itableLib);
UtilFuncs.addWatFuncs(watModule, 'find_index', module);
module.addFunctionExport('find_index', 'find_index');
UtilFuncs.addWatFuncs(watModule, 'find_type_by_index', module);
UtilFuncs.addWatFuncs(
watModule,
BuiltinNames.findPropertyFlagAndIndex,
module,
);
module.addFunctionExport(
BuiltinNames.findPropertyFlagAndIndex,
BuiltinNames.findPropertyFlagAndIndex,
);
UtilFuncs.addWatFuncs(watModule, BuiltinNames.findPropertyType, module);
watModule.dispose();
}
50 changes: 30 additions & 20 deletions src/backend/binaryen/lib/init_builtin_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ import {
UtilFuncs,
FunctionalFuncs,
FlattenLoop,
getCString,
getFieldFromMetaByOffset,
MetaDataOffset,
META_FLAG_MASK,
ItableFlag,
MetaFieldOffset,
MetaPropertyOffset,
SIZE_OF_META_FIELD,
getWASMObjectMeta,
} from '../utils.js';
import { dyntype } from './dyntype/utils.js';
import { arrayToPtr } from '../glue/transform.js';
Expand All @@ -51,7 +48,7 @@ function anyrefCond(module: binaryen.Module) {

const dynCtx = binaryenCAPI._BinaryenGlobalGet(
module.ptr,
getCString(dyntype.dyntype_context),
UtilFuncs.getCString(dyntype.dyntype_context),
dyntype.dyn_ctx_t,
);
const cond = module.call(
Expand All @@ -66,7 +63,7 @@ function anyrefCond(module: binaryen.Module) {
);
const extRef = binaryenCAPI._BinaryenTableGet(
module.ptr,
getCString(BuiltinNames.extrefTable),
UtilFuncs.getCString(BuiltinNames.extrefTable),
index,
binaryen.anyref,
);
Expand Down Expand Up @@ -124,14 +121,18 @@ function getPropNameThroughMeta(module: binaryen.Module) {
const statementArray: binaryen.ExpressionRef[] = [];

// 1. get meta
const metaValue = getWASMObjectMeta(module, obj);
const metaValue = FunctionalFuncs.getWASMObjectMeta(module, obj);
statementArray.push(module.local.set(metaIndex, metaValue));

// 2. get meta fields count
statementArray.push(
module.local.set(
metaFieldsCountIndex,
getFieldFromMetaByOffset(module, meta, MetaDataOffset.COUNT_OFFSET),
FunctionalFuncs.getFieldFromMetaByOffset(
module,
meta,
MetaDataOffset.COUNT_OFFSET,
),
),
);

Expand Down Expand Up @@ -160,7 +161,7 @@ function getPropNameThroughMeta(module: binaryen.Module) {
);
const loopStmtsArray: binaryen.ExpressionRef[] = [];
const flagAndIndex = module.i32.load(
MetaFieldOffset.FLAG_AND_INDEX_OFFSET,
MetaPropertyOffset.FLAG_AND_INDEX_OFFSET,
memoryAlignment,
metaFieldsPtr,
);
Expand Down Expand Up @@ -241,7 +242,7 @@ function getPropNameThroughMeta(module: binaryen.Module) {
module.local.set(
propNameIndex,
module.i32.load(
MetaFieldOffset.NAME_OFFSET,
MetaPropertyOffset.NAME_OFFSET,
memoryAlignment,
metaFieldsPtr,
),
Expand Down Expand Up @@ -2224,7 +2225,10 @@ function string_match_stringref(module: binaryen.Module) {
module.ptr,
stringArrayTypeInfoForStringRef.heapTypeRef,
module.i32.const(1),
binaryenCAPI._BinaryenStringConst(module.ptr, getCString('')),
binaryenCAPI._BinaryenStringConst(
module.ptr,
UtilFuncs.getCString(''),
),
),
),
);
Expand Down Expand Up @@ -2503,7 +2507,10 @@ function string_charAt_stringref(module: binaryen.Module) {
module.i32.ge_s(index_i32, len),
),
module.return(
binaryenCAPI._BinaryenStringConst(module.ptr, getCString('')),
binaryenCAPI._BinaryenStringConst(
module.ptr,
UtilFuncs.getCString(''),
),
),
),
);
Expand Down Expand Up @@ -3155,7 +3162,10 @@ function string_trim_stringref(module: binaryen.Module) {
module.ptr,
StringRefEqOp.EQ,
char,
binaryenCAPI._BinaryenStringConst(module.ptr, getCString(' ')),
binaryenCAPI._BinaryenStringConst(
module.ptr,
UtilFuncs.getCString(' '),
),
),
);
const loopStmts = module.block(null, [
Expand Down Expand Up @@ -3254,21 +3264,21 @@ function allocExtRefTableSlot(module: binaryen.Module) {
const arrName = getBuiltInFuncName(BuiltinNames.extRefTableMaskArr);
const maskArr = binaryenCAPI._BinaryenGlobalGet(
module.ptr,
getCString(arrName),
UtilFuncs.getCString(arrName),
charArrayTypeInfo.typeRef,
);
const newArray = binaryenCAPI._BinaryenArrayNew(
module.ptr,
charArrayTypeInfo.heapTypeRef,
binaryenCAPI._BinaryenTableSize(
module.ptr,
getCString(BuiltinNames.extrefTable),
UtilFuncs.getCString(BuiltinNames.extrefTable),
),
module.i32.const(0),
);
const tableGrow = binaryenCAPI._BinaryenTableGrow(
module.ptr,
getCString(BuiltinNames.extrefTable),
UtilFuncs.getCString(BuiltinNames.extrefTable),
binaryenCAPI._BinaryenRefNull(
module.ptr,
binaryenCAPI._BinaryenTypeStructref(),
Expand All @@ -3285,7 +3295,7 @@ function allocExtRefTableSlot(module: binaryen.Module) {
tableGrow,
binaryenCAPI._BinaryenGlobalSet(
module.ptr,
getCString(arrName),
UtilFuncs.getCString(arrName),
newArray,
),
]),
Expand Down Expand Up @@ -3358,7 +3368,7 @@ function allocExtRefTableSlot(module: binaryen.Module) {
ifStmts2.push(
binaryenCAPI._BinaryenGlobalSet(
module.ptr,
getCString(arrName),
UtilFuncs.getCString(arrName),
module.local.get(tmpMaskArrIdx, charArrayTypeInfo.typeRef),
),
);
Expand All @@ -3373,7 +3383,7 @@ function allocExtRefTableSlot(module: binaryen.Module) {
);
const tableSetOp = binaryenCAPI._BinaryenTableSet(
module.ptr,
getCString(BuiltinNames.extrefTable),
UtilFuncs.getCString(BuiltinNames.extrefTable),
module.local.get(tableIdx, binaryen.i32),
module.local.get(objIdx, binaryen.anyref),
);
Expand Down Expand Up @@ -3410,7 +3420,7 @@ function newExtRef(module: binaryen.Module) {
[
binaryenCAPI._BinaryenGlobalGet(
module.ptr,
getCString(dyntype.dyntype_context),
UtilFuncs.getCString(dyntype.dyntype_context),
binaryen.anyref,
),
tableIdx,
Expand Down
Loading

0 comments on commit 833babd

Please sign in to comment.