Skip to content

Commit

Permalink
update WASM3, adding docs to Rust APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed Mar 30, 2024
1 parent f1cce48 commit 6f315d9
Show file tree
Hide file tree
Showing 44 changed files with 3,024 additions and 408 deletions.
3 changes: 3 additions & 0 deletions Assets/Script/Lib/Dora/en/App.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ local record App
-- Should be in format of "v0.0.0".
const version: string

-- The dependencies of the game engine.
const deps: string

-- The time in seconds since the last frame update.
const deltaTime: number

Expand Down
2 changes: 1 addition & 1 deletion Assets/Script/Lib/Dora/en/Array.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ local record Array
index: function(self: Array, item: Item): integer

-- Removes and returns the last item in the array.
-- @return (Item) The last item in the array.
-- @return (Item) The last item removed from the array.
removeLast: function(self: Array): Item

-- Removes the first occurrence of a given item from the array without preserving order.
Expand Down
6 changes: 6 additions & 0 deletions Assets/Script/Lib/Dora/en/Node.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,12 @@ local record Node
-- @param y (integer) The y-index of the vertex in the grabber grid.
-- @param color (Color) The new color of the vertex.
setColor: function(self: Grabber, x: integer, y: integer, color: Color)

-- Sets the UV coordinates of a vertex in the grabber grid.
-- @param x (integer) The x-index of the vertex in the grabber grid.
-- @param y (integer) The y-index of the vertex in the grabber grid.
-- @param offset (Vec2) The new UV coordinates of the vertex.
moveUV: function(self: Grabber, x: integer, y: integer, offset: Vec2)
end

-- Creates or removes a texture grabber for the specified node.
Expand Down
3 changes: 3 additions & 0 deletions Assets/Script/Lib/Dora/zh-Hans/App.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ local record App
-- 格式为“v0.0.0”。
const version: string

-- 游戏引擎的第三方依赖库的版本信息。
const deps: string

-- 自从上一帧游戏更新以来间隔的时间(以秒为单位)。
const deltaTime: number

Expand Down
6 changes: 6 additions & 0 deletions Assets/Script/Lib/Dora/zh-Hans/Node.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ local record Node
-- @param y (integer) 顶点在抓取器网格中的 y 索引。
-- @param color (Color) 顶点的新颜色。
setColor: function(self: Grabber, x: integer, y: integer, color: Color)

-- 设置抓取器网格中一个顶点的纹理坐标。
-- @param x (integer) 顶点在抓取器网格中的 x 索引。
-- @param y (integer) 顶点在抓取器网格中的 y 索引。
-- @param offset (Vec2) 顶点的新纹理坐标。
moveUV: function(self: Grabber, x: integer, y: integer, offset: Vec2)
end

-- 创建或移除节点上的纹理抓取器。
Expand Down
63 changes: 55 additions & 8 deletions Source/3rdParty/wasm3/m3_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,12 @@ IM3Runtime m3_NewRuntime (IM3Environment i_environment, u32 i_stackSizeInBytes
runtime->environment = i_environment;
runtime->userdata = i_userdata;

runtime->stack = m3_Malloc ("Wasm Stack", i_stackSizeInBytes + 4*sizeof (m3slot_t)); // TODO: more precise stack checks
runtime->originStack = m3_Malloc ("Wasm Stack", i_stackSizeInBytes + 4*sizeof (m3slot_t)); // TODO: more precise stack checks

if (runtime->stack)
if (runtime->originStack)
{
runtime->numStackSlots = i_stackSizeInBytes / sizeof (m3slot_t); m3log (runtime, "new stack: %p", runtime->stack);
runtime->stack = runtime->originStack;
runtime->numStackSlots = i_stackSizeInBytes / sizeof (m3slot_t); m3log (runtime, "new stack: %p", runtime->originStack);
}
else m3_Free (runtime);
}
Expand Down Expand Up @@ -233,7 +234,7 @@ void Runtime_Release (IM3Runtime i_runtime)
Environment_ReleaseCodePages (i_runtime->environment, i_runtime->pagesOpen);
Environment_ReleaseCodePages (i_runtime->environment, i_runtime->pagesFull);

m3_Free (i_runtime->stack);
m3_Free (i_runtime->originStack);
m3_Free (i_runtime->memory.mallocated);
}

Expand Down Expand Up @@ -297,8 +298,12 @@ M3Result EvaluateExpression (IM3Module i_module, void * o_expressed, u8 i_type

if (not result)
{
# if (d_m3EnableOpProfiling || d_m3EnableOpTracing)
m3ret_t r = RunCode (m3code, stack, NULL, d_m3OpDefaultArgs, d_m3BaseCstr);
# else
m3ret_t r = RunCode (m3code, stack, NULL, d_m3OpDefaultArgs);

# endif

if (r == 0)
{ m3log (runtime, "expression result: %s", SPrintValue (stack, i_type));
if (SizeOfType (i_type) == sizeof (u32))
Expand All @@ -317,7 +322,7 @@ M3Result EvaluateExpression (IM3Module i_module, void * o_expressed, u8 i_type
}
else result = m3Err_mallocFailedCodePage;

runtime.stack = NULL; // prevent free(stack) in ReleaseRuntime
runtime.originStack = NULL; // prevent free(stack) in ReleaseRuntime
Runtime_Release (& runtime);
i_module->runtime = savedRuntime;

Expand Down Expand Up @@ -568,7 +573,11 @@ _ (CompileFunction (function));
startFunctionTmp = io_module->startFunction;
io_module->startFunction = -1;

# if (d_m3EnableOpProfiling || d_m3EnableOpTracing)
result = (M3Result) RunCode (function->compiled, (m3stack_t) runtime->stack, runtime->memory.mallocated, d_m3OpDefaultArgs, d_m3BaseCstr);
# else
result = (M3Result) RunCode (function->compiled, (m3stack_t) runtime->stack, runtime->memory.mallocated, d_m3OpDefaultArgs);
# endif

if (result)
{
Expand Down Expand Up @@ -666,8 +675,7 @@ M3Result m3_SetGlobal (IM3Global i_global,
const IM3TaggedValue i_value)
{
if (not i_global) return m3Err_globalLookupFailed;
// TODO: if (not g->isMutable) return m3Err_globalNotMutable;

if (not i_global->isMutable) return m3Err_globalNotMutable;
if (i_global->type != i_value->type) return m3Err_globalTypeMismatch;

switch (i_value->type) {
Expand Down Expand Up @@ -751,6 +759,31 @@ _ (CompileFunction (function))
return result;
}


M3Result m3_GetTableFunction (IM3Function * o_function, IM3Module i_module, uint32_t i_index)
{
_try {
if (i_index >= i_module->table0Size)
{
_throw ("function index out of range");
}

IM3Function function = i_module->table0[i_index];

if (function)
{
if (not function->compiled)
{
_ (CompileFunction (function))
}
}

* o_function = function;
} _catch:
return result;
}


static
M3Result checkStartFunction(IM3Module i_module)
{
Expand Down Expand Up @@ -875,7 +908,11 @@ _ (checkStartFunction(i_function->module))
}
}

# if (d_m3EnableOpProfiling || d_m3EnableOpTracing)
result = (M3Result) RunCode (i_function->compiled, (m3stack_t)(runtime->stack), runtime->memory.mallocated, d_m3OpDefaultArgs, d_m3BaseCstr);
# else
result = (M3Result) RunCode (i_function->compiled, (m3stack_t)(runtime->stack), runtime->memory.mallocated, d_m3OpDefaultArgs);
# endif
ReportNativeStackUsage ();

runtime->lastCalled = result ? NULL : i_function;
Expand Down Expand Up @@ -920,7 +957,12 @@ _ (checkStartFunction(i_function->module))
}
}

# if (d_m3EnableOpProfiling || d_m3EnableOpTracing)
result = (M3Result) RunCode (i_function->compiled, (m3stack_t)(runtime->stack), runtime->memory.mallocated, d_m3OpDefaultArgs, d_m3BaseCstr);
# else
result = (M3Result) RunCode (i_function->compiled, (m3stack_t)(runtime->stack), runtime->memory.mallocated, d_m3OpDefaultArgs);
# endif

ReportNativeStackUsage ();

runtime->lastCalled = result ? NULL : i_function;
Expand Down Expand Up @@ -965,7 +1007,12 @@ _ (checkStartFunction(i_function->module))
}
}

# if (d_m3EnableOpProfiling || d_m3EnableOpTracing)
result = (M3Result) RunCode (i_function->compiled, (m3stack_t)(runtime->stack), runtime->memory.mallocated, d_m3OpDefaultArgs, d_m3BaseCstr);
# else
result = (M3Result) RunCode (i_function->compiled, (m3stack_t)(runtime->stack), runtime->memory.mallocated, d_m3OpDefaultArgs);
# endif

ReportNativeStackUsage ();

runtime->lastCalled = result ? NULL : i_function;
Expand Down
4 changes: 4 additions & 0 deletions Source/3rdParty/wasm3/m3_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ typedef struct M3Module

IM3Function * table0;
u32 table0Size;
const char* table0ExportName;

M3MemoryInfo memoryInfo;
M3ImportInfo memoryImport;
bool memoryImported;
const char* memoryExportName;

//bool hasWasmCodeCopy;

Expand Down Expand Up @@ -168,6 +171,7 @@ typedef struct M3Runtime
IM3Module modules; // linked list of imported modules

void * stack;
void * originStack;
u32 stackSize;
u32 numStackSlots;
IM3Function lastCalled; // last function that successfully executed
Expand Down
33 changes: 27 additions & 6 deletions Source/3rdParty/wasm3/m3_exec.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ d_m3BeginExternC

#endif


# if (d_m3EnableOpProfiling || d_m3EnableOpTracing)
d_m3RetSig Call (d_m3OpSig, cstr_t i_operationName)
# else
d_m3RetSig Call (d_m3OpSig)
# endif
{
m3ret_t possible_trap = m3_Yield ();
if (M3_UNLIKELY(possible_trap)) return possible_trap;
Expand Down Expand Up @@ -149,8 +152,8 @@ d_m3CommutativeOpMacro(RES, REG, TYPE,NAME, OP, ##__VA_ARGS__)
#define d_m3CommutativeOpMacro_f(TYPE, NAME, MACRO, ...) d_m3CommutativeOpMacro (_fp0, _fp0, TYPE, NAME, MACRO, ##__VA_ARGS__)
#define d_m3OpMacro_f(TYPE, NAME, MACRO, ...) d_m3OpMacro (_fp0, _fp0, TYPE, NAME, MACRO, ##__VA_ARGS__)

#define M3_FUNC(RES, A, B, OP) (RES) = OP((A), (B)) // Accept functions: res = OP(a,b)
#define M3_OPER(RES, A, B, OP) (RES) = ((A) OP (B)) // Accept operators: res = a OP b
#define M3_FUNC(RES, A, B, OP) (RES) = OP((A), (B)) // Accept functions: res = OP(a,b)
#define M3_OPER(RES, A, B, OP) (RES) = ((A) OP (B)) // Accept operators: res = a OP b

#define d_m3CommutativeOpFunc_i(TYPE, NAME, OP) d_m3CommutativeOpMacro_i (TYPE, NAME, M3_FUNC, OP)
#define d_m3OpFunc_i(TYPE, NAME, OP) d_m3OpMacro_i (TYPE, NAME, M3_FUNC, OP)
Expand Down Expand Up @@ -193,10 +196,17 @@ d_m3CompareOp_f (f32, LessThanOrEqual, <=) d_m3CompareOp_f (f64, LessTh
d_m3CompareOp_f (f32, GreaterThanOrEqual, >=) d_m3CompareOp_f (f64, GreaterThanOrEqual, >=)
#endif

d_m3CommutativeOp_i (i32, Add, +) d_m3CommutativeOp_i (i64, Add, +)
d_m3CommutativeOp_i (i32, Multiply, *) d_m3CommutativeOp_i (i64, Multiply, *)
#define OP_ADD_32(A,B) (i32)((u32)(A) + (u32)(B))
#define OP_ADD_64(A,B) (i64)((u64)(A) + (u64)(B))
#define OP_SUB_32(A,B) (i32)((u32)(A) - (u32)(B))
#define OP_SUB_64(A,B) (i64)((u64)(A) - (u64)(B))
#define OP_MUL_32(A,B) (i32)((u32)(A) * (u32)(B))
#define OP_MUL_64(A,B) (i64)((u64)(A) * (u64)(B))

d_m3Op_i (i32, Subtract, -) d_m3Op_i (i64, Subtract, -)
d_m3CommutativeOpFunc_i (i32, Add, OP_ADD_32) d_m3CommutativeOpFunc_i (i64, Add, OP_ADD_64)
d_m3CommutativeOpFunc_i (i32, Multiply, OP_MUL_32) d_m3CommutativeOpFunc_i (i64, Multiply, OP_MUL_64)

d_m3OpFunc_i (i32, Subtract, OP_SUB_32) d_m3OpFunc_i (i64, Subtract, OP_SUB_64)

#define OP_SHL_32(X,N) ((X) << ((u32)(N) % 32))
#define OP_SHL_64(X,N) ((X) << ((u64)(N) % 64))
Expand Down Expand Up @@ -537,7 +547,12 @@ d_m3Op (Call)

m3stack_t sp = _sp + stackOffset;

# if (d_m3EnableOpProfiling || d_m3EnableOpTracing)
m3ret_t r = Call (callPC, sp, _mem, d_m3OpDefaultArgs, d_m3BaseCstr);
# else
m3ret_t r = Call (callPC, sp, _mem, d_m3OpDefaultArgs);
# endif

_mem = memory->mallocated;

if (M3_LIKELY(not r))
Expand Down Expand Up @@ -575,7 +590,13 @@ d_m3Op (CallIndirect)

if (M3_LIKELY(not r))
{

# if (d_m3EnableOpProfiling || d_m3EnableOpTracing)
r = Call (function->compiled, sp, _mem, d_m3OpDefaultArgs, d_m3BaseCstr);
# else
r = Call (function->compiled, sp, _mem, d_m3OpDefaultArgs);
# endif

_mem = memory->mallocated;

if (M3_LIKELY(not r))
Expand Down
21 changes: 17 additions & 4 deletions Source/3rdParty/wasm3/m3_exec_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ d_m3BeginExternC
# define d_m3BaseOpAllArgs _pc, _sp, _mem, _r0
# define d_m3BaseOpDefaultArgs 0
# define d_m3BaseClearRegisters _r0 = 0;
# define d_m3BaseCstr ""

# define d_m3ExpOpSig(...) d_m3BaseOpSig, __VA_ARGS__
# define d_m3ExpOpArgs(...) d_m3BaseOpArgs, __VA_ARGS__
Expand All @@ -42,18 +43,30 @@ d_m3BeginExternC
# define d_m3ClearRegisters d_m3BaseClearRegisters
# endif

typedef m3ret_t (vectorcall * IM3Operation) (d_m3OpSig);

#define d_m3RetSig static inline m3ret_t vectorcall
#define d_m3Op(NAME) M3_NO_UBSAN d_m3RetSig op_##NAME (d_m3OpSig)
# if (d_m3EnableOpProfiling || d_m3EnableOpTracing)
typedef m3ret_t (vectorcall * IM3Operation) (d_m3OpSig, cstr_t i_operationName);
# define d_m3Op(NAME) M3_NO_UBSAN d_m3RetSig op_##NAME (d_m3OpSig, cstr_t i_operationName)

#define nextOpImpl() ((IM3Operation)(* _pc))(_pc + 1, d_m3OpArgs)
#define jumpOpImpl(PC) ((IM3Operation)(* PC))( PC + 1, d_m3OpArgs)
# define nextOpImpl() ((IM3Operation)(* _pc))(_pc + 1, d_m3OpArgs, __FUNCTION__)
# define jumpOpImpl(PC) ((IM3Operation)(* PC))( PC + 1, d_m3OpArgs, __FUNCTION__)
# else
typedef m3ret_t (vectorcall * IM3Operation) (d_m3OpSig);
# define d_m3Op(NAME) M3_NO_UBSAN d_m3RetSig op_##NAME (d_m3OpSig)

# define nextOpImpl() ((IM3Operation)(* _pc))(_pc + 1, d_m3OpArgs)
# define jumpOpImpl(PC) ((IM3Operation)(* PC))( PC + 1, d_m3OpArgs)
# endif

#define nextOpDirect() M3_MUSTTAIL return nextOpImpl()
#define jumpOpDirect(PC) M3_MUSTTAIL return jumpOpImpl((pc_t)(PC))

# if (d_m3EnableOpProfiling || d_m3EnableOpTracing)
d_m3RetSig RunCode (d_m3OpSig, cstr_t i_operationName)
# else
d_m3RetSig RunCode (d_m3OpSig)
# endif
{
nextOpDirect();
}
Expand Down
1 change: 0 additions & 1 deletion Source/3rdParty/wasm3/m3_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ void dump_code_page (IM3CodePage i_codePage, pc_t i_startPC)

while (pc < end)
{
// pc_t operationPC = pc;
IM3Operation op = (IM3Operation) (* pc++);

OpInfo i = find_operation_info (op);
Expand Down
15 changes: 14 additions & 1 deletion Source/3rdParty/wasm3/m3_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ _ (Module_AddFunction (io_module, typeIndex, & import))
{
_ (ParseType_Memory (& io_module->memoryInfo, & i_bytes, i_end));
io_module->memoryImported = true;
io_module->memoryImport = import;
import = clearImport;
}
break;

Expand Down Expand Up @@ -256,6 +258,18 @@ _ (ReadLEB_u32 (& index, & i_bytes, i_end));
global->name = utf8;
utf8 = NULL; // ownership transferred to M3Global
}
else if (exportKind == d_externalKind_memory)
{
m3_Free (io_module->memoryExportName);
io_module->memoryExportName = utf8;
utf8 = NULL; // ownership transferred to M3Module
}
else if (exportKind == d_externalKind_table)
{
m3_Free (io_module->table0ExportName);
io_module->table0ExportName = utf8;
utf8 = NULL; // ownership transferred to M3Module
}

m3_Free (utf8);
}
Expand Down Expand Up @@ -342,7 +356,6 @@ _ (ReadLEB_u32 (& size, & i_bytes, i_end));

if (size)
{
// const u8 * ptr = i_bytes;
i_bytes += size;

if (i_bytes <= i_end)
Expand Down
3 changes: 3 additions & 0 deletions Source/3rdParty/wasm3/wasm3.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow")
M3Result m3_FindFunction (IM3Function * o_function,
IM3Runtime i_runtime,
const char * const i_functionName);
M3Result m3_GetTableFunction (IM3Function * o_function,
IM3Module i_module,
uint32_t i_index);

uint32_t m3_GetArgCount (IM3Function i_function);
uint32_t m3_GetRetCount (IM3Function i_function);
Expand Down
Loading

0 comments on commit 6f315d9

Please sign in to comment.