@@ -50,7 +50,8 @@ public static void Main(string[] args)
5050 GetCurrentPlatformRuntimeIdentifier ( ) ,
5151 "libnode" + GetSharedLibraryExtension ( ) ) ;
5252
53- private napi_env _env ;
53+ private NodeEmbeddingRuntime ? _runtime ;
54+ private NodeEmbeddingNodeApiScope ? _nodeApiScope ;
5455 private JSValue _jsString ;
5556 private JSFunction _jsFunction ;
5657 private JSFunction _jsFunctionWithArgs ;
@@ -64,6 +65,9 @@ public static void Main(string[] args)
6465 private JSFunction _jsFunctionCallMethod ;
6566 private JSFunction _jsFunctionCallMethodWithArgs ;
6667 private JSReference _reference = null ! ;
68+ private static readonly string [ ] s_settings = new [ ] { "node" , "--expose-gc" } ;
69+ private static string s_mainScript { get ; } =
70+ "globalThis.require = require('module').createRequire(process.execPath);\n " ;
6771
6872 /// <summary>
6973 /// Simple class that is exported to JS and used in some benchmarks.
@@ -84,16 +88,19 @@ public static void Method() { }
8488 /// </summary>
8589 protected void Setup ( )
8690 {
87- NodejsPlatform platform = new ( LibnodePath /*, args: new[] { "node", "--expose-gc" }*/ ) ;
88-
89- // This setup avoids using NodejsEnvironment so benchmarks can run on the same thread.
90- // NodejsEnvironment creates a separate thread that would slow down the micro-benchmarks.
91- platform . Runtime . CreateEnvironment (
92- platform , Console . WriteLine , null , NodejsEnvironment . NodeApiVersion , out _env )
93- . ThrowIfFailed ( ) ;
94-
95- // The new scope instance saves itself as the thread-local JSValueScope.Current.
96- JSValueScope scope = new ( JSValueScopeType . Root , _env , platform . Runtime ) ;
91+ NodeEmbeddingPlatform platform = new (
92+ LibnodePath ,
93+ new NodeEmbeddingPlatformSettings { Args = s_settings } ) ;
94+
95+ // This setup avoids using NodejsEmbeddingThreadRuntime so benchmarks can run on
96+ // the same thread. NodejsEmbeddingThreadRuntime creates a separate thread that would slow
97+ // down the micro-benchmarks.
98+ _runtime = NodeEmbeddingRuntime . Create ( platform ,
99+ new NodeEmbeddingRuntimeSettings { MainScript = s_mainScript } ) ;
100+
101+ // The nodeApiScope creates JSValueScope instance that saves itself as
102+ // the thread-local JSValueScope.Current.
103+ _nodeApiScope = new ( _runtime ) ;
97104
98105 // Create some JS values that will be used by the benchmarks.
99106
0 commit comments