| 2979 | // >>>>>>>> WasmEdge global instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
| 2980 | |
| 2981 | WASMEDGE_CAPI_EXPORT WasmEdge_GlobalInstanceContext * |
| 2982 | WasmEdge_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 >ype = *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 | |
| 3026 | WASMEDGE_CAPI_EXPORT const WasmEdge_GlobalTypeContext * |
| 3027 | WasmEdge_GlobalInstanceGetGlobalType( |