MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / TEST

Function TEST

test/externref/ExternrefTest.cpp:418–483  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

416}
417
418TEST(ExternRefTest, Ref__Functions) {
419 WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(nullptr, nullptr);
420 WasmEdge_ModuleInstanceContext *HostMod = createExternModule();
421 WasmEdge_Value P[4], R[1];
422 WasmEdge_String FuncName;
423
424 EXPECT_TRUE(
425 WasmEdge_ResultOK(WasmEdge_VMRegisterModuleFromImport(VMCxt, HostMod)));
426 char FuncsPath[] = "externrefTestData/funcs.wasm";
427 HexToFile(FuncsWasm, FuncsPath);
428 EXPECT_TRUE(WasmEdge_ResultOK(WasmEdge_VMLoadWasmFromFile(VMCxt, FuncsPath)));
429 EXPECT_TRUE(WasmEdge_ResultOK(WasmEdge_VMValidate(VMCxt)));
430 EXPECT_TRUE(WasmEdge_ResultOK(WasmEdge_VMInstantiate(VMCxt)));
431
432 // Functor instance
433 SquareStruct SS;
434 // Class instance
435 AddClass AC;
436
437 // Test 1: call add -- 1234 + 5678
438 P[0] = WasmEdge_ValueGenExternRef(&AC);
439 P[1] = WasmEdge_ValueGenI32(1234);
440 P[2] = WasmEdge_ValueGenI32(5678);
441 FuncName = WasmEdge_StringCreateByCString("call_add");
442 EXPECT_TRUE(
443 WasmEdge_ResultOK(WasmEdge_VMExecute(VMCxt, FuncName, P, 3, R, 1)));
444 WasmEdge_StringDelete(FuncName);
445 EXPECT_TRUE(WasmEdge_ValTypeIsI32(R[0].Type));
446 EXPECT_EQ(WasmEdge_ValueGetI32(R[0]), 6912);
447
448 // Test 2: call mul -- 789 * 4321
449 P[0] = WasmEdge_ValueGenExternRef(reinterpret_cast<void *>(&MulFunc));
450 P[1] = WasmEdge_ValueGenI32(789);
451 P[2] = WasmEdge_ValueGenI32(4321);
452 FuncName = WasmEdge_StringCreateByCString("call_mul");
453 EXPECT_TRUE(
454 WasmEdge_ResultOK(WasmEdge_VMExecute(VMCxt, FuncName, P, 3, R, 1)));
455 WasmEdge_StringDelete(FuncName);
456 EXPECT_TRUE(WasmEdge_ValTypeIsI32(R[0].Type));
457 EXPECT_EQ(WasmEdge_ValueGetI32(R[0]), 3409269);
458
459 // Test 3: call square -- 8256^2
460 P[0] = WasmEdge_ValueGenExternRef(&SS);
461 P[1] = WasmEdge_ValueGenI32(8256);
462 FuncName = WasmEdge_StringCreateByCString("call_square");
463 EXPECT_TRUE(
464 WasmEdge_ResultOK(WasmEdge_VMExecute(VMCxt, FuncName, P, 2, R, 1)));
465 WasmEdge_StringDelete(FuncName);
466 EXPECT_TRUE(WasmEdge_ValTypeIsI32(R[0].Type));
467 EXPECT_EQ(WasmEdge_ValueGetI32(R[0]), 68161536);
468
469 // Test 4: call sum and square -- (210 + 654)^2
470 P[0] = WasmEdge_ValueGenExternRef(&AC);
471 P[1] = WasmEdge_ValueGenExternRef(&SS);
472 P[2] = WasmEdge_ValueGenI32(210);
473 P[3] = WasmEdge_ValueGenI32(654);
474 FuncName = WasmEdge_StringCreateByCString("call_add_square");
475 EXPECT_TRUE(

Callers

nothing calls this directly

Calls 15

WasmEdge_VMCreateFunction · 0.85
createExternModuleFunction · 0.85
WasmEdge_ResultOKFunction · 0.85
WasmEdge_StringDeleteFunction · 0.85
WasmEdge_ValueGetI32Function · 0.85
WasmEdge_VMDeleteFunction · 0.85
WasmEdge_ValueGetF32Function · 0.85
WasmEdge_ValTypeIsRefFunction · 0.85
WasmEdge_ValueIsNullRefFunction · 0.85
strMethod · 0.80
insertMethod · 0.80

Tested by

no test coverage detected