diff --git a/src/Function.FromCallback.cs b/src/Function.FromCallback.cs index 03821e4..c45071c 100644 --- a/src/Function.FromCallback.cs +++ b/src/Function.FromCallback.cs @@ -39,10 +39,7 @@ public static Function FromCallback(Store store, Action callback) { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; callback( ); @@ -104,10 +101,7 @@ public static Function FromCallback(Store store, Action callback) { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; callback( convT.Unbox(storeContext, store, args_and_results[0])); @@ -170,10 +164,7 @@ public static Function FromCallback(Store store, Action callba { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1])); @@ -238,10 +229,7 @@ public static Function FromCallback(Store store, Action(Store store, Action(Store store, Action(Store store, Action< { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -530,10 +509,7 @@ public static Function FromCallback(Store store, Act { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -608,10 +584,7 @@ public static Function FromCallback(Store store, { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -688,10 +661,7 @@ public static Function FromCallback(Store st { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -770,10 +740,7 @@ public static Function FromCallback(Sto { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -854,10 +821,7 @@ public static Function FromCallback(Store store, Func callback { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( ); @@ -1083,10 +1041,7 @@ public static Function FromCallback(Store store, Func c { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT.Unbox(storeContext, store, args_and_results[0])); @@ -1151,10 +1106,7 @@ public static Function FromCallback(Store store, Func(Store store, Func(Store store, Func(Store store, Fu { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -1443,10 +1386,7 @@ public static Function FromCallback(Store store { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -1521,10 +1461,7 @@ public static Function FromCallback(Store s { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -1601,10 +1538,7 @@ public static Function FromCallback(Sto { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -1683,10 +1617,7 @@ public static Function FromCallback { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -1767,10 +1698,7 @@ public static Function FromCallback(Store store, Func(Store store, Func(Store store, Fun { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1])); @@ -2230,10 +2143,7 @@ public static Function FromCallback(Store store, { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -2304,10 +2214,7 @@ public static Function FromCallback(Store st { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -2380,10 +2287,7 @@ public static Function FromCallback(Stor { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -2458,10 +2362,7 @@ public static Function FromCallback( { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -2538,10 +2439,7 @@ public static Function FromCallback(Store store, F { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( ); @@ -3119,10 +2999,7 @@ public static Function FromCallback(Store store { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT.Unbox(storeContext, store, args_and_results[0])); @@ -3191,10 +3068,7 @@ public static Function FromCallback(Store { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1])); @@ -3265,10 +3139,7 @@ public static Function FromCallback(St { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT1.Unbox(storeContext, store, args_and_results[0]), convT2.Unbox(storeContext, store, args_and_results[1]), @@ -3341,10 +3212,7 @@ public static Function FromCallback(Stor { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( ); @@ -4176,10 +4017,7 @@ public static Function FromCallback(S { try { - var caller = new Caller(callerPtr); - var storeContext = caller.context; - var store = caller.store; - + var storeContext = store.Context; var result = callback( convT.Unbox(storeContext, store, args_and_results[0])); @@ -4250,10 +4088,7 @@ public static Function FromCallback { -<# GenerateCallbackContent(hasCaller, resultCount, parameterCount); #> +<# GenerateCallbackContent(hasCaller, resultCount, parameterCount, true); #> }; var funcType = CreateFunctionType(parameterKinds, resultKinds); diff --git a/src/FunctionCallbackOverloadTemplates.t4 b/src/FunctionCallbackOverloadTemplates.t4 index 3427ebf..eb42c62 100644 --- a/src/FunctionCallbackOverloadTemplates.t4 +++ b/src/FunctionCallbackOverloadTemplates.t4 @@ -4,14 +4,23 @@ // Suppress the "not used" warning when this file is referenced but the function is not called. #pragma warning disable CS8321 // Local function is declared but never used -void GenerateCallbackContent(bool hasCaller, int resultCount, int parameterCount) { +void GenerateCallbackContent(bool hasCaller, int resultCount, int parameterCount, bool storeAvailable) { #pragma warning restore CS8321 // Local function is declared but never used #> try { - var caller = new Caller(callerPtr); + <# if (storeAvailable && !hasCaller) + { + #>var storeContext = store.Context;<# + } + else + { + #>var caller = new Caller(callerPtr); var storeContext = caller.context; var store = caller.store; +<# + } + #> <#= resultCount > 0 ? "var result = " : "" #>callback( <# diff --git a/src/Linker.DefineFunction.tt b/src/Linker.DefineFunction.tt index d01a313..7ccc045 100644 --- a/src/Linker.DefineFunction.tt +++ b/src/Linker.DefineFunction.tt @@ -69,7 +69,7 @@ foreach (var (hasCaller, resultCount, parameterCount, methodGenerics, delegateTy { Function.Native.WasmtimeFuncUncheckedCallback func = (env, callerPtr, args_and_results, num_args_and_results) => { -<# GenerateCallbackContent(hasCaller, resultCount, parameterCount); #> +<# GenerateCallbackContent(hasCaller, resultCount, parameterCount, false); #> }; const int StackallocThreshold = 256; diff --git a/src/Store.cs b/src/Store.cs index 6378e6b..a119f1b 100644 --- a/src/Store.cs +++ b/src/Store.cs @@ -142,6 +142,8 @@ public Store(Engine engine, object? data) var storeHandle = GCHandle.Alloc(this, GCHandleType.Weak); handle = new Handle(Native.wasmtime_store_new(engine.NativeHandle, (IntPtr)storeHandle, Finalizer)); + + contextHandle = Native.wasmtime_store_context(NativeHandle); } /// @@ -257,6 +259,25 @@ public void SetEpochDeadline(ulong ticksBeyondCurrent) /// public void SetData(object? data) => this.data = data; + /// + public void Dispose() + { + handle.Dispose(); + } + + internal Handle NativeHandle + { + get + { + if (handle.IsInvalid) + { + throw new ObjectDisposedException(typeof(Store).FullName); + } + + return handle; + } + } + /// /// Gets the context of the store. /// @@ -267,24 +288,16 @@ public void SetEpochDeadline(ulong ticksBeyondCurrent) /// store handle in the GC finalizer thread while the is still /// in use. /// - internal StoreContext Context => new StoreContext(Native.wasmtime_store_context(NativeHandle)); - - /// - public void Dispose() - { - handle.Dispose(); - } - - internal Handle NativeHandle + internal StoreContext Context { get { - if (handle.IsInvalid) + if (handle.IsClosed) { throw new ObjectDisposedException(typeof(Store).FullName); } - return handle; + return new StoreContext(contextHandle); } } @@ -320,6 +333,7 @@ private static class Native public static extern void wasmtime_store_limiter(Handle store, long memory_size, long table_elements, long instances, long tables, long memories); } + private readonly IntPtr contextHandle; private readonly Handle handle; private object? data;