quick and dirty pure lua webassembly interpreter
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

code formatting

+97 -97
+97 -97
wasmlib.lua
··· 277 277 table.insert(self.stack, self:readarg64()) 278 278 end 279 279 280 + function wasmlib.VM:invoke(funcIndex) 281 + local fr = frame.StackFrame:new(funcIndex) 282 + local f = self.functions[funcIndex] 283 + local sig = self.types[f.typeidx] 284 + for i = 1, #sig.arguments do 285 + fr.locals[#sig.arguments - i] = table.remove(self.stack) 286 + end 287 + table.insert(self.stackFrames, fr) 288 + end 289 + 290 + function wasmlib.VM:ret() 291 + table.remove(self.stackFrames) 292 + end 293 + 280 294 function wasmlib.VM:step() 281 295 local curFrame = self:topFrame() 282 296 local body = self.functions[curFrame.funcIndex].body ··· 286 300 local c = constants.opcodes 287 301 local optable = { 288 302 -- control 289 - [c.OP_UNREACHABLE] = function() error("unreachable") end, 290 - [c.OP_NOP] = function() end, 303 + [c.OP_UNREACHABLE] = function() error("unreachable") end, 304 + [c.OP_NOP] = function() end, 291 305 -- BLOCK 292 306 -- LOOP 293 307 -- IF ··· 296 310 -- BR 297 311 -- BR_IF 298 312 -- BR_TABLE 299 - [c.OP_RETURN] = function() self:ret() end, 300 - [c.OP_CALL] = function() self:call() end, 313 + [c.OP_RETURN] = function() self:ret() end, 314 + [c.OP_CALL] = function() self:call() end, 301 315 -- CALL_INDIRECT 302 316 -- parametric 303 - [c.OP_DROP] = function() table.remove(self.stack) end, 304 - [c.OP_SELECT] = function() self:triop(ops.select) end, 317 + [c.OP_DROP] = function() table.remove(self.stack) end, 318 + [c.OP_SELECT] = function() self:triop(ops.select) end, 305 319 -- variable 306 - [c.OP_LOCAL_GET] = function() self:local_get() end, 307 - [c.OP_LOCAL_SET] = function() self:local_set() end, 308 - [c.OP_LOCAL_TEE] = function() self:local_tee() end, 309 - [c.OP_GLOBAL_GET] = function() self:global_get() end, 310 - [c.OP_GLOBAL_SET] = function() self:global_set() end, 320 + [c.OP_LOCAL_GET] = function() self:local_get() end, 321 + [c.OP_LOCAL_SET] = function() self:local_set() end, 322 + [c.OP_LOCAL_TEE] = function() self:local_tee() end, 323 + [c.OP_GLOBAL_GET] = function() self:global_get() end, 324 + [c.OP_GLOBAL_SET] = function() self:global_set() end, 311 325 -- memory 312 - [c.OP_I32_LOAD] = function() self:i32_load() end, 313 - [c.OP_I64_LOAD] = function() self:i64_load() end, 326 + [c.OP_I32_LOAD] = function() self:i32_load() end, 327 + [c.OP_I64_LOAD] = function() self:i64_load() end, 314 328 -- [float instrs] 315 - [c.OP_I32_LOAD8_S] = function() self:i32_load8_s() end, 316 - [c.OP_I32_LOAD8_U] = function() self:i32_load8_u() end, 317 - [c.OP_I32_LOAD16_S] = function() self:i32_load16_s() end, 318 - [c.OP_I32_LOAD16_U] = function() self:i32_load16_u() end, 319 - [c.OP_I64_LOAD8_S] = function() self:i64_load8_s() end, 320 - [c.OP_I64_LOAD8_U] = function() self:i64_load8_u() end, 321 - [c.OP_I64_LOAD16_S] = function() self:i64_load16_s() end, 322 - [c.OP_I64_LOAD16_U] = function() self:i64_load16_u() end, 323 - [c.OP_I64_LOAD32_S] = function() self:i64_load32_s() end, 324 - [c.OP_I64_LOAD32_U] = function() self:i64_load32_u() end, 325 - [c.OP_I32_STORE] = function() self:i32_store() end, 326 - [c.OP_I64_STORE] = function() self:i64_store() end, 329 + [c.OP_I32_LOAD8_S] = function() self:i32_load8_s() end, 330 + [c.OP_I32_LOAD8_U] = function() self:i32_load8_u() end, 331 + [c.OP_I32_LOAD16_S] = function() self:i32_load16_s() end, 332 + [c.OP_I32_LOAD16_U] = function() self:i32_load16_u() end, 333 + [c.OP_I64_LOAD8_S] = function() self:i64_load8_s() end, 334 + [c.OP_I64_LOAD8_U] = function() self:i64_load8_u() end, 335 + [c.OP_I64_LOAD16_S] = function() self:i64_load16_s() end, 336 + [c.OP_I64_LOAD16_U] = function() self:i64_load16_u() end, 337 + [c.OP_I64_LOAD32_S] = function() self:i64_load32_s() end, 338 + [c.OP_I64_LOAD32_U] = function() self:i64_load32_u() end, 339 + [c.OP_I32_STORE] = function() self:i32_store() end, 340 + [c.OP_I64_STORE] = function() self:i64_store() end, 327 341 -- [floart instrs] 328 - [c.OP_I32_STORE8] = function() self:i32_store8() end, 329 - [c.OP_I32_STORE16] = function() self:i32_store16() end, 330 - [c.OP_I64_STORE8] = function() self:i64_store8() end, 331 - [c.OP_I64_STORE16] = function() self:i64_store16() end, 332 - [c.OP_I64_STORE32] = function() self:i64_store32() end, 333 - [c.OP_MEMORY_SIZE] = function() self:memory_size() end, 334 - [c.OP_MEMORY_GROW] = function() self:memory_grow() end, 342 + [c.OP_I32_STORE8] = function() self:i32_store8() end, 343 + [c.OP_I32_STORE16] = function() self:i32_store16() end, 344 + [c.OP_I64_STORE8] = function() self:i64_store8() end, 345 + [c.OP_I64_STORE16] = function() self:i64_store16() end, 346 + [c.OP_I64_STORE32] = function() self:i64_store32() end, 347 + [c.OP_MEMORY_SIZE] = function() self:memory_size() end, 348 + [c.OP_MEMORY_GROW] = function() self:memory_grow() end, 335 349 -- constants 336 - [c.OP_I32_CONST] = function() self:i32_const() end, 337 - [c.OP_I64_CONST] = function() self:i64_const() end, 350 + [c.OP_I32_CONST] = function() self:i32_const() end, 351 + [c.OP_I64_CONST] = function() self:i64_const() end, 338 352 -- [float consts] 339 353 -- i32 comparisons 340 - [c.OP_I32_EQZ] = function() self:unop (ops.i32_eqz) end, 341 - [c.OP_I32_EQ] = function() self:binop(ops.i32_eq) end, 342 - [c.OP_I32_NE] = function() self:binop(ops.i32_ne) end, 343 - [c.OP_I32_LT_S] = function() self:binop(ops.i32_lt_s) end, 344 - [c.OP_I32_LT_U] = function() self:binop(ops.i32_lt_u) end, 345 - [c.OP_I32_GT_S] = function() self:binop(ops.i32_gt_s) end, 346 - [c.OP_I32_GT_U] = function() self:binop(ops.i32_gt_u) end, 347 - [c.OP_I32_LE_S] = function() self:binop(ops.i32_le_s) end, 348 - [c.OP_I32_LE_U] = function() self:binop(ops.i32_le_u) end, 354 + [c.OP_I32_EQZ] = function() self:unop (ops.i32_eqz) end, 355 + [c.OP_I32_EQ] = function() self:binop(ops.i32_eq) end, 356 + [c.OP_I32_NE] = function() self:binop(ops.i32_ne) end, 357 + [c.OP_I32_LT_S] = function() self:binop(ops.i32_lt_s) end, 358 + [c.OP_I32_LT_U] = function() self:binop(ops.i32_lt_u) end, 359 + [c.OP_I32_GT_S] = function() self:binop(ops.i32_gt_s) end, 360 + [c.OP_I32_GT_U] = function() self:binop(ops.i32_gt_u) end, 361 + [c.OP_I32_LE_S] = function() self:binop(ops.i32_le_s) end, 362 + [c.OP_I32_LE_U] = function() self:binop(ops.i32_le_u) end, 349 363 -- i64 comparisons 350 - [c.OP_I64_EQZ] = function() self:unop (ops.i64_eqz) end, 351 - [c.OP_I64_EQ] = function() self:binop(ops.i64_eq) end, 352 - [c.OP_I64_NE] = function() self:binop(ops.i64_ne) end, 353 - [c.OP_I64_LT_S] = function() self:binop(ops.i64_lt_s) end, 364 + [c.OP_I64_EQZ] = function() self:unop (ops.i64_eqz) end, 365 + [c.OP_I64_EQ] = function() self:binop(ops.i64_eq) end, 366 + [c.OP_I64_NE] = function() self:binop(ops.i64_ne) end, 367 + [c.OP_I64_LT_S] = function() self:binop(ops.i64_lt_s) end, 354 368 -- I64_LT_U 355 - [c.OP_I64_GT_S] = function() self:binop(ops.i64_gt_s) end, 369 + [c.OP_I64_GT_S] = function() self:binop(ops.i64_gt_s) end, 356 370 -- I64_GT_U 357 - [c.OP_I64_LE_S] = function() self:binop(ops.i64_le_s) end, 371 + [c.OP_I64_LE_S] = function() self:binop(ops.i64_le_s) end, 358 372 -- I64_LE_U 359 - [c.OP_I64_GE_S] = function() self:binop(ops.i64_ge_s) end, 373 + [c.OP_I64_GE_S] = function() self:binop(ops.i64_ge_s) end, 360 374 -- I64_GE_U 361 375 -- [float comparisons] 362 376 -- i32 operations 363 - [c.OP_I32_CLZ] = function() self:unop (ops.i32_clz) end, 364 - [c.OP_I32_CTZ] = function() self:unop (ops.i32_ctz) end, 365 - [c.OP_I32_POPCNT] = function() self:unop (ops.i32_popcnt) end, 366 - [c.OP_I32_ADD] = function() self:binop(ops.i32_add) end, 367 - [c.OP_I32_SUB] = function() self:binop(ops.i32_sub) end, 368 - [c.OP_I32_MUL] = function() self:binop(ops.i32_mul) end, 369 - [c.OP_I32_DIV_S] = function() self:binop(ops.i32_div_s) end, 370 - [c.OP_I32_DIV_U] = function() self:binop(ops.i32_div_u) end, 371 - [c.OP_I32_REM_S] = function() self:binop(ops.i32_rem_s) end, 372 - [c.OP_I32_REM_U] = function() self:binop(ops.i32_rem_u) end, 373 - [c.OP_I32_AND] = function() self:binop(ops.i32_and) end, 374 - [c.OP_I32_OR] = function() self:binop(ops.i32_or) end, 375 - [c.OP_I32_XOR] = function() self:binop(ops.i32_xor) end, 376 - [c.OP_I32_SHL] = function() self:binop(ops.i32_shl) end, 377 - [c.OP_I32_SHR_S] = function() self:binop(ops.i32_shr_s) end, 378 - [c.OP_I32_SHR_U] = function() self:binop(ops.i32_shr_u) end, 379 - [c.OP_I32_ROTL] = function() self:binop(ops.i32_rotl) end, 380 - [c.OP_I32_ROTR] = function() self:binop(ops.i32_rotr) end, 377 + [c.OP_I32_CLZ] = function() self:unop (ops.i32_clz) end, 378 + [c.OP_I32_CTZ] = function() self:unop (ops.i32_ctz) end, 379 + [c.OP_I32_POPCNT] = function() self:unop (ops.i32_popcnt) end, 380 + [c.OP_I32_ADD] = function() self:binop(ops.i32_add) end, 381 + [c.OP_I32_SUB] = function() self:binop(ops.i32_sub) end, 382 + [c.OP_I32_MUL] = function() self:binop(ops.i32_mul) end, 383 + [c.OP_I32_DIV_S] = function() self:binop(ops.i32_div_s) end, 384 + [c.OP_I32_DIV_U] = function() self:binop(ops.i32_div_u) end, 385 + [c.OP_I32_REM_S] = function() self:binop(ops.i32_rem_s) end, 386 + [c.OP_I32_REM_U] = function() self:binop(ops.i32_rem_u) end, 387 + [c.OP_I32_AND] = function() self:binop(ops.i32_and) end, 388 + [c.OP_I32_OR] = function() self:binop(ops.i32_or) end, 389 + [c.OP_I32_XOR] = function() self:binop(ops.i32_xor) end, 390 + [c.OP_I32_SHL] = function() self:binop(ops.i32_shl) end, 391 + [c.OP_I32_SHR_S] = function() self:binop(ops.i32_shr_s) end, 392 + [c.OP_I32_SHR_U] = function() self:binop(ops.i32_shr_u) end, 393 + [c.OP_I32_ROTL] = function() self:binop(ops.i32_rotl) end, 394 + [c.OP_I32_ROTR] = function() self:binop(ops.i32_rotr) end, 381 395 -- i64 operations 382 - [c.OP_I64_CLZ] = function() self:unop (ops.i64_clz) end, 383 - [c.OP_I64_CTZ] = function() self:unop (ops.i64_ctz) end, 384 - [c.OP_I64_POPCNT] = function() self:unop (ops.i64_popcnt) end, 385 - [c.OP_I64_ADD] = function() self:binop(ops.i64_add) end, 386 - [c.OP_I64_SUB] = function() self:binop(ops.i64_sub) end, 387 - [c.OP_I64_MUL] = function() self:binop(ops.i64_mul) end, 388 - [c.OP_I64_DIV_S] = function() self:binop(ops.i64_div_s) end, 396 + [c.OP_I64_CLZ] = function() self:unop (ops.i64_clz) end, 397 + [c.OP_I64_CTZ] = function() self:unop (ops.i64_ctz) end, 398 + [c.OP_I64_POPCNT] = function() self:unop (ops.i64_popcnt) end, 399 + [c.OP_I64_ADD] = function() self:binop(ops.i64_add) end, 400 + [c.OP_I64_SUB] = function() self:binop(ops.i64_sub) end, 401 + [c.OP_I64_MUL] = function() self:binop(ops.i64_mul) end, 402 + [c.OP_I64_DIV_S] = function() self:binop(ops.i64_div_s) end, 389 403 -- I64_DIV_U 390 - [c.OP_I64_REM_S] = function() self:binop(ops.i64_rem_s) end, 404 + [c.OP_I64_REM_S] = function() self:binop(ops.i64_rem_s) end, 391 405 -- I64_REM_U 392 - [c.OP_I64_AND] = function() self:binop(ops.i64_and) end, 393 - [c.OP_I64_OR] = function() self:binop(ops.i64_or) end, 394 - [c.OP_I64_XOR] = function() self:binop(ops.i64_xor) end, 395 - [c.OP_I64_SHL] = function() self:binop(ops.i64_shl) end, 396 - [c.OP_I64_SHR_S] = function() self:binop(ops.i64_shr_s) end, 406 + [c.OP_I64_AND] = function() self:binop(ops.i64_and) end, 407 + [c.OP_I64_OR] = function() self:binop(ops.i64_or) end, 408 + [c.OP_I64_XOR] = function() self:binop(ops.i64_xor) end, 409 + [c.OP_I64_SHL] = function() self:binop(ops.i64_shl) end, 410 + [c.OP_I64_SHR_S] = function() self:binop(ops.i64_shr_s) end, 397 411 -- I64_SHR_U 398 - [c.OP_I64_ROTL] = function() self:binop(ops.i64_rotl) end, 399 - [c.OP_I64_ROTR] = function() self:binop(ops.i64_rotr) end, 412 + [c.OP_I64_ROTL] = function() self:binop(ops.i64_rotl) end, 413 + [c.OP_I64_ROTR] = function() self:binop(ops.i64_rotr) end, 400 414 -- [float operations] 401 415 -- conversions 402 416 [c.OP_I32_WRAP_I64] = function() self:unop(ops.i32_wrap_i64) end, ··· 411 425 error("unimplemented") 412 426 end 413 427 opfunc() 414 - end 415 - 416 - function wasmlib.VM:invoke(funcIndex) 417 - local fr = frame.StackFrame:new(funcIndex) 418 - local f = self.functions[funcIndex] 419 - local sig = self.types[f.typeidx] 420 - for i = 1, #sig.arguments do 421 - fr.locals[#sig.arguments - i] = table.remove(self.stack) 422 - end 423 - table.insert(self.stackFrames, fr) 424 - end 425 - 426 - function wasmlib.VM:ret() 427 - table.remove(self.stackFrames) 428 428 end 429 429 430 430 return wasmlib