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

Function WasmEdge_GlobalInstanceCreate

lib/api/wasmedge.cpp:2981–3024  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2979// >>>>>>>> WasmEdge global instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2980
2981WASMEDGE_CAPI_EXPORT WasmEdge_GlobalInstanceContext *
2982WasmEdge_GlobalInstanceCreate(const WasmEdge_GlobalTypeContext *GlobType,
2983 const WasmEdge_Value Value) noexcept {
2984 try {
2985 if (GlobType) {
2986 // Comparison of the value types needs the module instance to retrieve the
2987 // function type index after applying the typed function reference
2988 // proposal. It's impossible to do this without refactoring. Therefore
2989 // simply match the FuncRef and ExternRef here.
2990 const AST::GlobalType &GType = *fromGlobTypeCxt(GlobType);
2991 WasmEdge::ValType ExpType = GType.getValType();
2992 WasmEdge::ValType GotType = genValType(Value.Type);
2993 if (ExpType.isFuncRefType() != GotType.isFuncRefType()) {
2994 spdlog::error(WasmEdge::ErrCode::Value::SetValueErrorType);
2995 spdlog::error(WasmEdge::ErrInfo::InfoMismatch(ExpType, GotType));
2996 return nullptr;
2997 }
2998
2999 WasmEdge::ValVariant Val =
3000 to_WasmEdge_128_t<WasmEdge::uint128_t>(Value.Value);
3001 if (ExpType.isRefType()) {
3002 // Reference type case.
3003 if (!ExpType.isNullableRefType() &&
3004 Val.get<WasmEdge::RefVariant>().isNull()) {
3005 // If this global is not a nullable ref type, the data should not be
3006 // null.
3007 spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired);
3008 return nullptr;
3009 }
3010 } else {
3011 // Number type case.
3012 if (ExpType != GotType) {
3013 spdlog::error(WasmEdge::ErrCode::Value::SetValueErrorType);
3014 return nullptr;
3015 }
3016 }
3017 return toGlobCxt(
3018 new WasmEdge::Runtime::Instance::GlobalInstance(GType, Val));
3019 }
3020 } catch (...) {
3021 handleCAPIError();
3022 }
3023 return nullptr;
3024}
3025
3026WASMEDGE_CAPI_EXPORT const WasmEdge_GlobalTypeContext *
3027WasmEdge_GlobalInstanceGetGlobalType(

Callers 2

createSpecTestModuleFunction · 0.85
TESTFunction · 0.85

Calls 9

InfoMismatchClass · 0.85
handleCAPIErrorFunction · 0.85
getValTypeMethod · 0.80
isFuncRefTypeMethod · 0.80
isRefTypeMethod · 0.80
isNullableRefTypeMethod · 0.80
isNullMethod · 0.80
genValTypeFunction · 0.70
errorFunction · 0.50

Tested by 1

TESTFunction · 0.68