Skip to content

Commit 1b708bf

Browse files
authored
Remove JSNativeApi.cs (#187)
1 parent a9b983a commit 1b708bf

40 files changed

Lines changed: 1904 additions & 1549 deletions

bench/Benchmarks.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static void Main(string[] args)
5151
"libnode" + GetSharedLibraryExtension());
5252

5353
private napi_env _env;
54+
private JSValue _jsString;
5455
private JSFunction _jsFunction;
5556
private JSFunction _jsFunctionWithArgs;
5657
private JSFunction _jsFunctionWithCallback;
@@ -95,13 +96,14 @@ protected void Setup()
9596

9697
// Create some JS values that will be used by the benchmarks.
9798

98-
_jsFunction = (JSFunction)JSNativeApi.RunScript("function jsFunction() { }; jsFunction");
99-
_jsFunctionWithArgs = (JSFunction)JSNativeApi.RunScript(
99+
_jsString = JSValue.RunScript("'Hello Node-API .Net!'");
100+
_jsFunction = (JSFunction)JSValue.RunScript("function jsFunction() { }; jsFunction");
101+
_jsFunctionWithArgs = (JSFunction)JSValue.RunScript(
100102
"function jsFunctionWithArgs(a, b, c) { }; jsFunctionWithArgs");
101-
_jsFunctionWithCallback = (JSFunction)JSNativeApi.RunScript(
103+
_jsFunctionWithCallback = (JSFunction)JSValue.RunScript(
102104
"function jsFunctionWithCallback(cb, ...args) { cb(...args); }; " +
103105
"jsFunctionWithCallback");
104-
_jsInstance = (JSObject)JSNativeApi.RunScript(
106+
_jsInstance = (JSObject)JSValue.RunScript(
105107
"const jsInstance = { method: (...args) => {} }; jsInstance");
106108

107109
_dotnetFunction = (JSFunction)JSValue.CreateFunction(
@@ -125,15 +127,15 @@ protected void Setup()
125127
(x, value) => x.Property = (string)value);
126128
classBuilder.AddMethod("method", (x) => (args) => DotnetClass.Method());
127129
_dotnetClass = (JSObject)classBuilder.DefineClass();
128-
_dotnetInstance = (JSObject)JSNativeApi.CallAsConstructor(_dotnetClass);
130+
_dotnetInstance = (JSObject)((JSValue)_dotnetClass).CallAsConstructor();
129131

130-
_jsFunctionCreateInstance = (JSFunction)JSNativeApi.RunScript(
132+
_jsFunctionCreateInstance = (JSFunction)JSValue.RunScript(
131133
"function jsFunctionCreateInstance(Class) { new Class() }; " +
132134
"jsFunctionCreateInstance");
133-
_jsFunctionCallMethod = (JSFunction)JSNativeApi.RunScript(
135+
_jsFunctionCallMethod = (JSFunction)JSValue.RunScript(
134136
"function jsFunctionCallMethod(instance) { instance.method(); }; " +
135137
"jsFunctionCallMethod");
136-
_jsFunctionCallMethodWithArgs = (JSFunction)JSNativeApi.RunScript(
138+
_jsFunctionCallMethodWithArgs = (JSFunction)JSValue.RunScript(
137139
"function jsFunctionCallMethodWithArgs(instance, ...args) " +
138140
"{ instance.method(...args); }; " +
139141
"jsFunctionCallMethodWithArgs");
@@ -145,6 +147,18 @@ protected void Setup()
145147

146148
// Benchmarks in the base class run in both CLR and AOT environments.
147149

150+
[Benchmark]
151+
public void JSValueToString()
152+
{
153+
_jsString.GetValueStringUtf16();
154+
}
155+
156+
[Benchmark]
157+
public void JSValueToStringAsCharArray()
158+
{
159+
_ = new string(_jsString.GetValueStringUtf16AsCharArray());
160+
}
161+
148162
[Benchmark]
149163
public void CallJSFunction()
150164
{
@@ -229,13 +243,13 @@ public class Clr : Benchmarks
229243
JSObject hostModule = new();
230244
_ = new ManagedHost(hostModule);
231245
_jsHost = hostModule;
232-
_jsFunctionCallMethodDynamic = (JSFunction)JSNativeApi.RunScript(
246+
_jsFunctionCallMethodDynamic = (JSFunction)JSValue.RunScript(
233247
"function jsFunctionCallMethodDynamic(dotnet) " +
234248
"{ dotnet.System.Object.ReferenceEquals(null, null); }; " +
235249
"jsFunctionCallMethodDynamic");
236250

237251
// Implement IFormatProvider in JS and pass it to a .NET method.
238-
_jsFunctionCallMethodDynamicInterface = (JSFunction)JSNativeApi.RunScript(
252+
_jsFunctionCallMethodDynamicInterface = (JSFunction)JSValue.RunScript(
239253
"function jsFunctionCallMethodDynamicInterface(dotnet) {" +
240254
" const formatProvider = { GetFormat: (type) => null };" +
241255
" dotnet.System.String.Format(formatProvider, '', null, null);" +

src/NodeApi.DotNetHost/JSMarshaller.cs

Lines changed: 109 additions & 89 deletions
Large diffs are not rendered by default.

src/NodeApi.DotNetHost/JSMarshallerDelegates.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Linq;
76
using System.Linq.Expressions;
87
using System.Reflection;

src/NodeApi.DotNetHost/ManagedHost.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7-
using System.Diagnostics.CodeAnalysis;
87
using System.Globalization;
98
using System.IO;
109
using System.Linq;
@@ -409,7 +408,7 @@ public JSValue LoadModule(JSCallbackArgs args)
409408

410409
if (exports.IsObject())
411410
{
412-
exportsRef = JSNativeApi.CreateReference(exports);
411+
exportsRef = new JSReference(exports);
413412
_loadedModules.Add(assemblyFilePath, exportsRef);
414413
}
415414

src/NodeApi.DotNetHost/TypeExporter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Linq.Expressions;
99
using System.Reflection;
1010
using System.Threading.Tasks;
11-
using System.Xml.Linq;
1211
using Microsoft.JavaScript.NodeApi.Interop;
1312

1413
using static Microsoft.JavaScript.NodeApi.DotNetHost.ManagedHost;

src/NodeApi.Generator/TypeDefinitionsGenerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.IO;
88
using System.Linq;
99
using System.Reflection;
10-
using System.Runtime.InteropServices;
1110
using System.Text;
1211
using System.Text.RegularExpressions;
1312
using System.Threading;

src/NodeApi/Interop/JSAsyncScope.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.Threading;
65

76
namespace Microsoft.JavaScript.NodeApi.Interop;
87

src/NodeApi/Interop/JSClassBuilderOfT.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public JSValue DefineClass(JSValue? baseClass = null)
8787
}
8888
else
8989
{
90-
classObject = JSNativeApi.DefineClass(
90+
classObject = JSValue.DefineClass(
9191
ClassName,
9292
new JSCallbackDescriptor(
9393
ClassName,
@@ -181,7 +181,7 @@ public JSValue DefineInterface()
181181

182182
AddTypeToString();
183183

184-
JSValue obj = JSNativeApi.DefineClass(
184+
JSValue obj = JSValue.DefineClass(
185185
ClassName,
186186
new JSCallbackDescriptor(ClassName, (args) =>
187187
{

src/NodeApi/Interop/JSRuntimeContext.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Threading;
1111
using Microsoft.JavaScript.NodeApi.Runtime;
1212
using static Microsoft.JavaScript.NodeApi.Interop.JSCollectionProxies;
13-
using static Microsoft.JavaScript.NodeApi.JSNativeApi;
1413
using static Microsoft.JavaScript.NodeApi.Runtime.JSRuntime;
1514

1615
namespace Microsoft.JavaScript.NodeApi.Interop;
@@ -20,7 +19,7 @@ namespace Microsoft.JavaScript.NodeApi.Interop;
2019
/// </summary>
2120
/// <remarks>
2221
/// A <see cref="JSRuntimeContext"/> instance is constructed when the .NET Node API managed host is
23-
/// loaded, and disposed when the host is unloaded. (For AOT there is no "host" compnoent, so each
22+
/// loaded, and disposed when the host is unloaded. (For AOT there is no "host" component, so each
2423
/// AOT module has a context that matches the module lifetime.) The context tracks several kinds
2524
/// of JS references used internally by this assembly, so that the references can be re-used for
2625
/// the lifetime of the host and disposed when the context is disposed.
@@ -124,7 +123,7 @@ public static explicit operator napi_env(JSRuntimeContext context)
124123
}
125124

126125
public static explicit operator JSRuntimeContext(napi_env env)
127-
=> GetInstanceData(env) as JSRuntimeContext
126+
=> JSValue.GetInstanceData(env) as JSRuntimeContext
128127
?? throw new InvalidCastException("Context is not found in napi_env instance data.");
129128

130129
public bool IsDisposed { get; private set; }
@@ -149,7 +148,7 @@ internal JSRuntimeContext(
149148

150149
_env = env;
151150
Runtime = runtime;
152-
SetInstanceData(env, this);
151+
JSValue.SetInstanceData(env, this);
153152
SynchronizationContext = synchronizationContext ?? JSSynchronizationContext.Create();
154153
}
155154

@@ -260,7 +259,7 @@ internal JSValue InitializeObjectWrapper(JSValue wrapper, JSValue externalInstan
260259
// The reference returned by Wrap() is weak (refcount=0), which is good:
261260
// if the JS object is released then the reference will fail to resolve, and
262261
// GetOrCreateObjectWrapper() will create a new JS wrapper if requested.
263-
JSNativeApi.Wrap(wrapper, obj, out JSReference wrapperWeakRef);
262+
wrapper.Wrap(obj, out JSReference wrapperWeakRef);
264263

265264
_objectMap.AddOrUpdate(
266265
obj,
@@ -558,7 +557,7 @@ public JSValue CreateStruct<T>() where T : struct
558557
throw new InvalidOperationException("Failed to resolve struct constructor reference.");
559558
}
560559

561-
return JSNativeApi.CallAsConstructor(constructorFunction.Value);
560+
return constructorFunction.Value.CallAsConstructor();
562561
}
563562

564563
/// <summary>

src/NodeApi/Interop/JSStructBuilderOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public JSValue DefineStruct()
100100
AddTypeToString();
101101

102102
// Note this does not use Wrap() because structs are passed by value.
103-
JSValue classObject = JSNativeApi.DefineClass(
103+
JSValue classObject = JSValue.DefineClass(
104104
StructName,
105105
new JSCallbackDescriptor(StructName, (args) => args.ThisArg),
106106
Properties.ToArray());

0 commit comments

Comments
 (0)