-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_runtime.cpp
More file actions
1144 lines (1027 loc) · 41.8 KB
/
node_runtime.cpp
File metadata and controls
1144 lines (1027 loc) · 41.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "utils/node_runtime.h"
#include "register_builtin.gen.h"
#include "register_classes.gen.h"
#include "utility_functions/utility_functions.h"
#include <cppgc/platform.h>
#include <node.h>
#include <node_api.h>
#include <uv.h>
#ifdef WIN32
#undef CONNECT_DEFERRED
#endif
#include "utils/value_convert.h"
#include <godot_cpp/classes/dir_access.hpp>
#include <godot_cpp/classes/file_access.hpp>
#include <godot_cpp/classes/json.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
#include <memory>
#include <string>
#include <vector>
#ifdef WIN32
#include <windows.h>
#endif
namespace gode {
static bool node_initialized = false;
static std::unique_ptr<node::MultiIsolatePlatform> platform;
static std::unique_ptr<node::ArrayBufferAllocator> allocator;
static node::IsolateData *isolate_data = nullptr;
static thread_local napi_env thread_local_env = nullptr;
v8::Isolate *NodeRuntime::isolate = nullptr;
node::Environment *NodeRuntime::env = nullptr;
v8::Global<v8::Context> NodeRuntime::node_context;
static Napi::Value fs_readFile(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (info.Length() < 1 || !info[0].IsString()) {
return env.Null();
}
std::string path = info[0].As<Napi::String>().Utf8Value();
if (path.find("res://") != 0) {
return env.Null();
}
godot::Ref<godot::FileAccess> file = godot::FileAccess::open(path.c_str(), godot::FileAccess::READ);
if (file.is_null()) {
return env.Null();
}
uint64_t len = file->get_length();
godot::PackedByteArray pba = file->get_buffer(len);
return Napi::String::New(env, reinterpret_cast<const char *>(pba.ptr()), len);
}
static Napi::Value fs_stat(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (info.Length() < 1 || !info[0].IsString()) {
return Napi::Number::New(env, 0);
}
std::string path = info[0].As<Napi::String>().Utf8Value();
if (path.find("res://") != 0) {
return Napi::Number::New(env, 0);
}
godot::String gd_path = godot::String::utf8(path.c_str());
if (godot::FileAccess::file_exists(gd_path)) {
return Napi::Number::New(env, 1); // File
}
if (godot::DirAccess::dir_exists_absolute(gd_path)) {
return Napi::Number::New(env, 2); // Directory
}
return Napi::Number::New(env, 0); // Not found
}
static Napi::Value Export(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
return env.Undefined();
}
// 预加载目录中的所有 .dll,确保依赖链在 llama-addon.node 加载前已解析
// 同时将 Release 目录和 CUDA bin 目录加入 PATH,确保传递依赖(如 cudart64_*.dll)可被找到
static Napi::Value preload_dlls(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
#ifdef WIN32
if (info.Length() < 1 || !info[0].IsString()) {
return env.Undefined();
}
std::string dir_utf8 = info[0].As<Napi::String>().Utf8Value();
auto to_wide = [](const std::string &s) -> std::wstring {
int n = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, nullptr, 0);
std::wstring w(n, 0);
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, &w[0], n);
return w;
};
// 把 Release 目录加到 PATH(影响 LOAD_WITH_ALTERED_SEARCH_PATH 的传递依赖搜索)
std::string dirs_to_add = dir_utf8;
// 找到 CUDA_PATH 并把 bin 加入 PATH(cudart64_*.dll、cublas64_*.dll 等)
char cuda_buf[4096] = {};
if (GetEnvironmentVariableA("CUDA_PATH", cuda_buf, sizeof(cuda_buf)) > 0) {
dirs_to_add += ";" + std::string(cuda_buf) + "\\bin";
}
// 同时尝试常见的版本号变体(CUDA_PATH_V12_0 等)
for (const char *varname : {"CUDA_PATH_V12_6", "CUDA_PATH_V12_5", "CUDA_PATH_V12_4",
"CUDA_PATH_V12_3", "CUDA_PATH_V12_2", "CUDA_PATH_V12_1", "CUDA_PATH_V12_0",
"CUDA_PATH_V11_8", "CUDA_PATH_V11_7"}) {
char buf[4096] = {};
if (GetEnvironmentVariableA(varname, buf, sizeof(buf)) > 0) {
dirs_to_add += ";" + std::string(buf) + "\\bin";
break;
}
}
// 更新 PATH
char path_buf[32767] = {};
GetEnvironmentVariableA("PATH", path_buf, sizeof(path_buf));
std::string new_path = dirs_to_add + ";" + std::string(path_buf);
SetEnvironmentVariableA("PATH", new_path.c_str());
// SetDllDirectory 让后续所有 LoadLibraryExW 都能搜索 Release 目录(包括传递依赖)
SetDllDirectoryW(to_wide(dir_utf8).c_str());
// 预加载 Release 目录中的每个 .dll
std::wstring wdir = to_wide(dir_utf8);
std::wstring pattern = wdir + L"\\*.dll";
WIN32_FIND_DATAW fd;
HANDLE h = FindFirstFileW(pattern.c_str(), &fd);
if (h != INVALID_HANDLE_VALUE) {
do {
std::wstring dll_path = wdir + L"\\" + fd.cFileName;
HMODULE hm = LoadLibraryExW(dll_path.c_str(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
if (!hm) LoadLibraryExW(dll_path.c_str(), nullptr, 0);
} while (FindNextFileW(h, &fd));
FindClose(h);
}
#endif
return env.Undefined();
}
#ifdef WIN32
// win_delay_load_hook.cc(由 cmake-js 编译进每个 NAPI addon)在 dliStartProcessing 时
// 调用 GetModuleHandleA("node.dll")。若找不到则 fallback 到 GetModuleHandle(NULL)
// 即 GodotEngine.exe,而 GodotEngine.exe 不导出 NAPI 函数 → 崩溃。
// 修复:加载与 libnode.dll 同目录的 node.dll(由构建系统生成的纯转发存根 DLL),
// 使 GetModuleHandleA("node.dll") 命中,GetProcAddress 返回正确的 NAPI 地址。
static void preload_gode_node_exe() {
HMODULE libnode = GetModuleHandleW(L"libnode.dll");
if (!libnode) return;
wchar_t libnode_path[MAX_PATH];
if (!GetModuleFileNameW(libnode, libnode_path, MAX_PATH)) return;
wchar_t node_dll_path[MAX_PATH];
wcscpy_s(node_dll_path, MAX_PATH, libnode_path);
wchar_t *sep = wcsrchr(node_dll_path, L'\\');
if (!sep) return;
wcscpy_s(sep + 1, MAX_PATH - (DWORD)(sep + 1 - node_dll_path), L"node.dll");
// 加载纯转发存根 node.dll(无 V8 代码,所有导出 forward 到 libnode)
LoadLibraryW(node_dll_path);
}
#endif
static Napi::Object InitGodeAddon(Napi::Env env, Napi::Object exports) {
#ifdef WIN32
preload_gode_node_exe();
#endif
thread_local_env = env;
gode::register_builtin(env, exports);
gode::register_classes(env, exports);
gode::GD::init(env, exports);
exports.Set("fs_readFile", Napi::Function::New(env, fs_readFile));
exports.Set("fs_stat", Napi::Function::New(env, fs_stat));
exports.Set("preload_dlls", Napi::Function::New(env, preload_dlls));
Napi::Object global = env.Global();
global.Set("Export", Napi::Function::New(env, Export));
global.Set("Signal", Napi::Function::New(env, Export)); // @Signal 装饰器,运行时为空操作,由 tree-sitter 静态解析
global.Set("Tool", Napi::Function::New(env, Export)); // @Tool 装饰器,运行时为空操作,由 tree-sitter 静态解析
return exports;
}
static napi_value InitGodeAddon_C(napi_env env, napi_value exports) {
return Napi::RegisterModule(env, exports, InitGodeAddon);
}
void NodeRuntime::init_once() {
if (node_initialized) {
return;
}
std::vector<std::string> args;
std::vector<std::string> exec_args;
std::vector<std::string> errors;
// args[0] 是程序名,后面的是 node 标志
args.push_back("godot node");
args.push_back("--experimental-vm-modules");
int flags = node::ProcessInitializationFlags::kNoInitializeV8 |
node::ProcessInitializationFlags::kNoInitializeNodeV8Platform |
node::ProcessInitializationFlags::kNoInitializeCppgc |
node::ProcessInitializationFlags::kNoDefaultSignalHandling |
node::ProcessInitializationFlags::kNoStdioInitialization;
auto init_result = node::InitializeOncePerProcess(args, static_cast<node::ProcessInitializationFlags::Flags>(flags));
if (!init_result->errors().empty()) {
for (const auto &err : init_result->errors()) {
godot::UtilityFunctions::printerr(godot::String("Node init error: ") + err.c_str());
}
}
allocator = node::ArrayBufferAllocator::Create();
platform = node::MultiIsolatePlatform::Create(4);
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
cppgc::InitializeProcess(platform->GetPageAllocator());
isolate = node::NewIsolate(allocator.get(), uv_default_loop(), platform.get());
{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = node::NewContext(isolate);
v8::Context::Scope context_scope(context);
isolate_data = node::CreateIsolateData(isolate, uv_default_loop(), platform.get(), allocator.get());
env = node::CreateEnvironment(isolate_data, context, args, exec_args);
node::AddLinkedBinding(env, "godot", InitGodeAddon_C, NODE_API_DEFAULT_MODULE_API_VERSION);
std::string boot_script =
"const Module = require('module');"
"const path = require('path');"
"const fs = require('fs');"
"const gode = process._linkedBinding('godot');"
"const isRes = p => typeof p === 'string' && p.startsWith('res://');"
"const toPosix = p => isRes(p) ? '/' + p.substring(6) : p;"
"const fromPosix = p => 'res://' + p.substring(1);"
"const tryExtensions = (p) => {"
" const exts = ['.js', '.json', '.node'];"
" for (const ext of exts) {"
" if (fs.existsSync(p + ext)) return p + ext;"
" }"
" return null;"
"};"
"const findInRes = (base, req) => {"
" const p = path.join(base, req);"
" if (fs.existsSync(p) && fs.statSync(p).isFile()) return p;"
" const withExt = tryExtensions(p);"
" if (withExt) return withExt;"
" if (fs.existsSync(p) && fs.statSync(p).isDirectory()) {"
" const pkgPath = path.join(p, 'package.json');"
" if (fs.existsSync(pkgPath)) {"
" try {"
" const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));"
" if (pkg.main) {"
" const main = path.resolve(p, pkg.main);"
" if (fs.existsSync(main) && fs.statSync(main).isFile()) return main;"
" const mainExt = tryExtensions(main);"
" if (mainExt) return mainExt;"
" const mainIndex = path.join(main, 'index.js');"
" if (fs.existsSync(mainIndex)) return mainIndex;"
" }"
" } catch(e){}"
" }"
" const index = path.join(p, 'index.js');"
" if (fs.existsSync(index)) return index;"
" }"
" return null;"
"};"
"try {"
" const _gode_module = new Module('godot');"
" _gode_module.id = 'godot';"
" _gode_module.exports = gode;"
" _gode_module.loaded = true;"
" _gode_module.filename = 'godot';"
" Module._cache['godot'] = _gode_module;"
"} catch (e) {"
" console.error('[Gode] Injection error:', e);"
"}"
""
"const originalReadFileSync = fs.readFileSync;"
"fs.readFileSync = function(p, options) {"
" if (typeof p === 'string' && p.startsWith('res://')) {"
" const content = gode.fs_readFile(p);"
" if (content !== null) return content;"
" }"
" return originalReadFileSync.call(fs, p, options);"
"};"
""
"const originalExistsSync = fs.existsSync;"
"fs.existsSync = function(p) {"
" if (typeof p === 'string' && p.startsWith('res://')) {"
" return gode.fs_stat(p) > 0;"
" }"
" return originalExistsSync ? originalExistsSync.call(fs, p) : false;"
"};"
""
"const originalStatSync = fs.statSync;"
"fs.statSync = function(p, options) {"
" if (typeof p === 'string' && p.startsWith('res://')) {"
" const type = gode.fs_stat(p);"
" if (type > 0) {"
" return {"
" isDirectory: () => type === 2,"
" isFile: () => type === 1,"
" mtime: new Date(),"
" size: 0"
" };"
" }"
" }"
" return originalStatSync.call(fs, p, options);"
"};"
""
"const originalJoin = path.join;"
"const originalResolve = path.resolve;"
"const originalDirname = path.dirname;"
""
"path.join = function(...args) {"
" if (args.length > 0 && isRes(args[0])) {"
" const mapped = args.map(a => isRes(a) ? '/' + a.substring(6) : a);"
" return fromPosix(path.posix.join(...mapped));"
" }"
" return originalJoin.apply(path, args);"
"};"
""
"path.resolve = function(...args) {"
" if (args.length > 0 && isRes(args[0])) {"
" const mapped = args.map(a => isRes(a) ? '/' + a.substring(6) : a);"
" return fromPosix(path.posix.resolve(...mapped));"
" }"
" return originalResolve.apply(path, args);"
"};"
""
"path.dirname = function(p) {"
" if (isRes(p)) {"
" return fromPosix(path.posix.dirname('/' + p.substring(6)));"
" }"
" return originalDirname.call(path, p);"
"};"
""
"const originalNodeModulePaths = Module._nodeModulePaths;"
"Module._nodeModulePaths = function(from) {"
" if (isRes(from)) {"
" const paths = [];"
" let p = from;"
" while (true) {"
" paths.push(p + (p.endsWith('/') ? '' : '/') + 'node_modules');"
" const parent = path.dirname(p);"
" if (parent === p) break;"
" if (p === 'res://') break;"
" p = parent;"
" }"
" return paths;"
" }"
" return originalNodeModulePaths.call(Module, from);"
"};"
""
"const originalFindPath = Module._findPath;"
"Module._findPath = function(request, paths, isMain) {"
" if (request.startsWith('res://')) {"
" return request;"
" }"
" const resPaths = paths.filter(p => typeof p === 'string' && p.startsWith('res://'));"
" for (const base of resPaths) {"
" const found = findInRes(base, request);"
" if (found) return found;"
" }"
" return originalFindPath.call(Module, request, paths, isMain);"
"};"
""
"const originalResolveFilename = Module._resolveFilename;"
"Module._resolveFilename = function(request, parent, isMain, options) {"
" if (request.startsWith('res://')) {"
" return request;"
" }"
" if (parent && parent.filename && parent.filename.startsWith('res://')) {"
" if (request.startsWith('./') || request.startsWith('../')) {"
" const parentDir = path.dirname(parent.filename);"
" const base = path.resolve(parentDir, request);"
" if (fs.existsSync(base) && fs.statSync(base).isFile()) return base;"
" for (const ext of ['.js', '.json', '.node']) {"
" if (fs.existsSync(base + ext)) return base + ext;"
" }"
" for (const idx of ['/index.js', '/index.json']) {"
" if (fs.existsSync(base + idx)) return base + idx;"
" }"
" return base;"
" }"
" try {"
" return originalResolveFilename.call(Module, request, parent, isMain, options);"
" } catch(e) {"
" const found = findInRes('res://node_modules', request);"
" if (found) return found;"
" throw e;"
" }"
" }"
" return originalResolveFilename.call(Module, request, parent, isMain, options);"
"};"
""
"globalThis.__gode_compile = function(code, filename) {"
" try {"
" const m = new Module(filename, module);"
" m.filename = filename;"
" m.paths = Module._nodeModulePaths(path.dirname(filename));"
" m._compile(code, filename);"
" return m.exports;"
" } catch (e) {"
" console.error('Gode compile error:', e);"
" return e;"
" }"
"};"
"const originalRequire = Module.prototype.require;"
"Module.prototype.require = function(id) {"
" if (id === 'godot') {"
" return gode;"
" }"
" return originalRequire.call(this, id);"
"};"
"const originalGlobalRequire = globalThis.require || Module.createRequire(process.cwd());"
"globalThis.require = function(id) {"
""
" if (id === 'godot') {"
" return gode;"
" }"
" return originalGlobalRequire.call(this, id);"
"};"
"if (originalGlobalRequire) Object.assign(globalThis.require, originalGlobalRequire);"
""
// patch process.dlopen:.node 原生模块必须用真实 OS 路径加载
// res://xxx 路径传给 LoadLibraryW 会导致地址错误和 NAPI init 崩溃
// 加载 .node 前将其目录追加到 PATH,让 Windows 能找到同目录的 DLL(如 ggml-cuda.dll)
"const _originalDlopen = process.dlopen;"
"process.dlopen = function(mod, filename, flags) {"
" let realPath = filename;"
" if (typeof filename === 'string') {"
" let p = filename;"
" if (p.startsWith('file://')) { try { p = require('url').fileURLToPath(p); } catch(_) {} }" // file:// → OS path
" if (p.startsWith('res://')) { p = require('path').join(process.cwd(), p.slice(6)); }"
" if (p.startsWith('\\\\\\\\?\\\\')) p = p.slice(4);" // 剥离 \\?\ 前缀
" realPath = p;"
" }"
" if (typeof realPath === 'string' && realPath.endsWith('.node') && typeof gode.preload_dlls === 'function') {"
" try { gode.preload_dlls(require('path').dirname(realPath)); } catch(_) {}"
" }"
" return _originalDlopen(mod, realPath, flags);"
"};"
""
"if (gode.GDObject && gode.GDObject.prototype) {"
" gode.GDObject.prototype.to_signal = function(signal, { timeoutMs, abortSignal } = {}) {"
" const obj = this;"
" return new Promise((resolve, reject) => {"
" let done = false;"
" let timer = null;"
" const cleanup = () => {"
" if (timer) clearTimeout(timer);"
" if (abortSignal) abortSignal.removeEventListener('abort', onAbort);"
" try { obj.disconnect(signal, callback); } catch (_) {}"
" };"
" const callback = (...args) => {"
" if (done) return;"
" done = true;"
" cleanup();"
" resolve(args.length <= 1 ? args[0] : args);"
" };"
" const onAbort = () => {"
" if (done) return;"
" done = true;"
" cleanup();"
" reject(new Error('to_signal: aborted'));"
" };"
" try {"
" obj.connect(signal, callback);"
" } catch (e) {"
" cleanup();"
" return reject(e);"
" }"
" if (abortSignal) abortSignal.addEventListener('abort', onAbort, { once: true });"
" if (typeof timeoutMs === 'number' && timeoutMs > 0) {"
" timer = setTimeout(() => {"
" if (done) return;"
" done = true;"
" cleanup();"
" reject(new Error(`to_signal: timeout waiting for '${signal}'`));"
" }, timeoutMs);"
" }"
" });"
" };"
"}"
// patch child_process.fork:拦截 testBindingBinary 的 fork,用本进程内模拟成功,
// 避免在 Windows 上 fork 出一个 GodotEngine.exe 子进程来做 addon 兼容性测试。
";"
"(function() {"
" try {"
" const cp = require('child_process');"
" const _origFork = cp.fork;"
" cp.fork = function(modulePath, args, options) {"
" if (typeof modulePath === 'string' && modulePath.includes('testBindingBinary')) {"
" const { EventEmitter } = require('events');"
" const mock = new EventEmitter();"
" mock.pid = 0; mock.exitCode = null; mock.killed = false;"
" mock.stdout = null; mock.stderr = null;"
" mock.kill = function() {};"
" mock.send = function(msg) {"
" const self = this;"
" if (msg && msg.type === 'start') {"
" process.nextTick(() => self.emit('message', { type: 'loaded' }));"
" } else if (msg && msg.type === 'test') {"
" process.nextTick(() => self.emit('message', { type: 'done' }));"
" } else if (msg && msg.type === 'exit') {"
" process.nextTick(() => { mock.exitCode = 0; self.emit('exit', 0, null); });"
" }"
" return true;"
" };"
" process.nextTick(() => mock.emit('message', { type: 'ready' }));"
" return mock;"
" }"
" return _origFork.apply(cp, arguments);"
" };"
" } catch(e) {}"
"})();";
node::LoadEnvironment(env, boot_script.c_str());
// 事件循环跑一次,确保 boot_script 执行完毕
isolate->PerformMicrotaskCheckpoint();
uv_run(uv_default_loop(), UV_RUN_ONCE);
// ESM 支持代码单独执行,确保在 boot_script 之后注册
std::string esm_script =
"(function() {"
"const vm = require('vm');"
"const fs = require('fs');"
"const path = require('path');"
""
"global.__gode_esm_supported = (typeof vm.SourceTextModule !== 'undefined');"
""
"global.__gode_esm_cache = new Map();" // filepath -> namespace (fully evaluated)
"global.__gode_esm_pending = new Map();" // filepath -> Promise<namespace>
"global.__gode_esm_mod_cache = new Map();" // filepath/specifier -> vm.Module (for linker)
"global.__gode_cjs_pending = new Map();" // resolvedPath -> Promise<SyntheticModule> (dedup)
""
// __gode_resolve_to_module: 将 specifier 解析为 vm.Module。
// 关键:对 ESM 文件只创建 SourceTextModule 并返回,不调用 link()。
// Node.js 的 [kLink] 机制会异步递归地对返回的模块调用同一个 linker,
// 从而避免同步递归导致的调用栈溢出。
// 将 res:// 路径转为 file:// URL,供 import.meta.url 使用
// fileURLToPath() 只认 file:// scheme,所以必须转换
"const _gode_res_to_file_url = (p) => {"
" if (!p || !p.startsWith('res://')) return p;"
" const rel = p.slice(6);" // 去掉 'res://'
" const abs = require('path').join(process.cwd(), rel).replace(/\\\\/g, '/');"
" return 'file:///' + abs;"
"};"
""
// 在 res://node_modules/ 里手动查找包入口,用于 require.resolve 无法处理 res:// 路径时的兜底
"function __gode_resolve_in_res(specifier, referrerPath) {"
" const parts = specifier.split('/');"
" const pkgName = parts[0].startsWith('@') ? parts[0] + '/' + parts[1] : parts[0];"
" const subPath = parts.slice(pkgName.split('/').length).join('/');"
" let searchDirs = [];"
" let d = path.dirname(referrerPath);"
" while (true) {"
" searchDirs.push(path.join(d, 'node_modules'));"
" const p = path.dirname(d); if (p === d) break; d = p;"
" }"
" for (const base of searchDirs) {"
" const pkgDir = path.join(base, pkgName);"
" if (!fs.existsSync(pkgDir)) continue;"
" if (subPath) {"
" const direct = path.join(pkgDir, subPath);"
" if (fs.existsSync(direct) && fs.statSync(direct).isFile()) return direct;"
" for (const ext of ['.js', '.mjs', '.json']) {"
" if (fs.existsSync(direct + ext)) return direct + ext;"
" }"
" return null;"
" }"
" const pkgJsonPath = path.join(pkgDir, 'package.json');"
" if (!fs.existsSync(pkgJsonPath)) continue;"
" let pkg; try { pkg = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8')); } catch(_) { continue; }"
" const exp = pkg.exports;"
" if (exp) {"
" const dot = exp['.'];"
" const entry = typeof dot === 'string' ? dot"
" : (dot && (dot.import || dot.module || dot.require || dot.default))"
" || (typeof exp === 'string' ? exp : null);"
" if (entry) { const r = path.resolve(pkgDir, entry); if (fs.existsSync(r)) return r; }"
" }"
" const main = pkg.module || pkg.main || 'index.js';"
" const r = path.resolve(pkgDir, main);"
" if (fs.existsSync(r)) return r;"
" for (const ext of ['.js', '.mjs']) {"
" if (fs.existsSync(r + ext)) return r + ext;"
" }"
" }"
" return null;"
"}"
""
// 解析 package.json imports 字段中的 #subpath 条目
"function __gode_resolve_pkg_import(specifier, referrerPath) {"
" let dir = path.dirname(referrerPath);"
" while (true) {"
" const pkgPath = path.join(dir, 'package.json');"
" if (fs.existsSync(pkgPath)) {"
" try {"
" const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));"
" if (pkg.imports) {"
" const entry = pkg.imports[specifier];"
" if (entry) {"
" const target = typeof entry === 'string' ? entry"
" : (entry.node || entry.require || entry.default || null);"
" if (target) return path.resolve(dir, target);"
" }"
" }"
" } catch(_) {}"
" }"
" const parent = path.dirname(dir);"
" if (parent === dir) break;"
" dir = parent;"
" }"
" return null;"
"}"
""
"global.__gode_resolve_to_module = async function(specifier, referrerPath) {"
" let resolvedPath;"
" if (specifier.startsWith('#')) {"
" const pkgImport = __gode_resolve_pkg_import(specifier, referrerPath);"
" if (!pkgImport) throw new Error(`Cannot find module '${specifier}' from '${referrerPath}'`);"
" resolvedPath = pkgImport;"
" } else if (specifier.startsWith('./') || specifier.startsWith('../')) {"
" const dir = path.dirname(referrerPath);"
" resolvedPath = path.resolve(dir, specifier);"
" if (!fs.existsSync(resolvedPath)) {"
" for (const ext of ['.mjs', '.js', '.json']) {"
" if (fs.existsSync(resolvedPath + ext)) { resolvedPath = resolvedPath + ext; break; }"
" }"
" }"
" } else if (specifier.startsWith('res://')) {"
" resolvedPath = specifier;"
" } else {"
" let _cjsMod; try { _cjsMod = require(specifier); } catch(_) {}"
" if (_cjsMod !== undefined) {"
" if (global.__gode_esm_mod_cache.has(specifier)) return global.__gode_esm_mod_cache.get(specifier);"
" const exportNames = Object.keys(_cjsMod);"
" const names = exportNames.includes('default') ? exportNames : ['default', ...exportNames];"
" const synMod = new vm.SyntheticModule(names, function() {"
" this.setExport('default', _cjsMod);"
" for (const key of exportNames) { this.setExport(key, _cjsMod[key]); }"
" }, { identifier: specifier });"
" await synMod.link(() => {});"
" await synMod.evaluate();"
" global.__gode_esm_mod_cache.set(specifier, synMod);"
" return synMod;"
" }"
" try {"
" resolvedPath = require.resolve(specifier, { paths: [path.dirname(referrerPath)] });"
" } catch(e) {"
" resolvedPath = __gode_resolve_in_res(specifier, referrerPath);"
" if (!resolvedPath) {"
" console.error('[gode] resolve failed: ' + specifier + ' from ' + referrerPath);"
" throw new Error(`Cannot find module '${specifier}' from '${referrerPath}'`);"
" }"
" }"
" }"
" if (global.__gode_esm_mod_cache.has(resolvedPath)) {"
" return global.__gode_esm_mod_cache.get(resolvedPath);"
" }"
" let source;"
" try {"
" source = fs.readFileSync(resolvedPath, 'utf8');"
" } catch(e) {"
" console.error('[gode] readFileSync failed: ' + resolvedPath + ' => ' + e.message);"
" throw e;"
" }"
" const isESM = resolvedPath.endsWith('.mjs') ||"
" (resolvedPath.endsWith('.js') && /^\\s*(import|export)\\s+/m.test(source));"
" if (isESM) {"
" let mod;"
" try {"
" mod = new vm.SourceTextModule(source, {"
" identifier: resolvedPath,"
" initializeImportMeta(meta) { meta.url = _gode_res_to_file_url(resolvedPath); },"
" importModuleDynamically: async (spec, ref) => {"
" return await global.__gode_resolve_to_module(spec, ref.identifier);"
" }"
" });"
" } catch(e) {"
" console.error('[gode] SourceTextModule parse error in ' + resolvedPath + ': ' + e.message);"
" throw e;"
" }"
" global.__gode_esm_mod_cache.set(resolvedPath, mod);"
" return mod;"
" } else {"
" if (global.__gode_cjs_pending.has(resolvedPath)) return global.__gode_cjs_pending.get(resolvedPath);"
" const cjsPromise = (async () => {"
" let cjsModule;"
" try {"
" cjsModule = require(resolvedPath);"
" } catch(e) {"
" console.error('[gode] require failed: ' + resolvedPath + ' => ' + e.message);"
" throw e;"
" }"
" const expNames = Object.keys(cjsModule);"
" const allNames = expNames.includes('default') ? expNames : ['default', ...expNames];"
" const synMod = new vm.SyntheticModule(allNames,"
" function() {"
" this.setExport('default', cjsModule);"
" for (const key of expNames) { this.setExport(key, cjsModule[key]); }"
" }, { identifier: resolvedPath });"
" await synMod.link(() => {});"
" await synMod.evaluate();"
" global.__gode_esm_mod_cache.set(resolvedPath, synMod);"
" global.__gode_cjs_pending.delete(resolvedPath);"
" return synMod;"
" })();"
" global.__gode_cjs_pending.set(resolvedPath, cjsPromise);"
" return cjsPromise;"
" }"
"};"
""
"global.__gode_load_esm = async function(filepath, source) {"
" if (!global.__gode_esm_supported) {"
" throw new Error('vm.SourceTextModule is not available');"
" }"
" if (global.__gode_esm_cache.has(filepath)) { return global.__gode_esm_cache.get(filepath); }"
" if (global.__gode_esm_pending.has(filepath)) { return global.__gode_esm_pending.get(filepath); }"
" const loadPromise = (async () => {"
" const module = new vm.SourceTextModule(source, {"
" identifier: filepath,"
" initializeImportMeta(meta) { meta.url = _gode_res_to_file_url(filepath); },"
" importModuleDynamically: async (specifier, referrer) => {"
" return await global.__gode_resolve_to_module(specifier, referrer.identifier);"
" }"
" });"
" global.__gode_esm_mod_cache.set(filepath, module);"
" try {"
" await module.link(async (specifier, referencingModule) => {"
" return await global.__gode_resolve_to_module(specifier, referencingModule.identifier);"
" });"
" } catch(e) {"
" console.error('[gode] link error in ' + filepath + ': ' + e.message);"
" if (e.stack) console.error(e.stack);"
" throw e;"
" }"
" try {"
" await module.evaluate();"
" } catch(e) {"
" console.error('[gode] evaluate error in ' + filepath + ': ' + e.message);"
" if (e.stack) console.error(e.stack);"
" throw e;"
" }"
" const ns = module.namespace;"
" global.__gode_esm_cache.set(filepath, ns);"
" global.__gode_esm_pending.delete(filepath);"
" return ns;"
" })();"
" global.__gode_esm_pending.set(filepath, loadPromise);"
" return loadPromise;"
"};"
""
"global.__gode_compile_esm = async function(code, filename) {"
" try {"
" const ns = await global.__gode_load_esm(filename, code);"
" return ns;"
" } catch (e) {"
" console.error('[gode] compile_esm FAILED: ' + filename + ' => ' + (e && e.message));"
" if (e && e.stack) console.error(e.stack);"
" return e;"
" }"
"};"
"})();";
// 执行 ESM 支持脚本
{
v8::Local<v8::String> esm_source = v8::String::NewFromUtf8(
isolate, esm_script.c_str(), v8::NewStringType::kNormal)
.ToLocalChecked();
v8::Local<v8::String> esm_name = v8::String::NewFromUtf8Literal(isolate, "<gode-esm-init>");
v8::ScriptOrigin esm_origin(esm_name);
v8::Local<v8::Script> esm_compiled;
if (v8::Script::Compile(context, esm_source, &esm_origin).ToLocal(&esm_compiled)) {
esm_compiled->Run(context);
} else {
// printf("[Gode ESM] Failed to compile ESM init script\n");
}
}
node_context.Reset(isolate, context);
}
node_initialized = true;
}
void NodeRuntime::run_script(const std::string &code) {
if (!node_initialized) {
init_once();
}
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = node_context.Get(isolate);
v8::Context::Scope context_scope(context);
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, code.c_str(), v8::NewStringType::kNormal, static_cast<int>(code.size())).ToLocalChecked();
v8::Local<v8::String> name = v8::String::NewFromUtf8(isolate, "<godot>", v8::NewStringType::kNormal).ToLocalChecked();
v8::ScriptOrigin origin(name);
v8::Local<v8::Script> script;
if (!v8::Script::Compile(context, source, &origin).ToLocal(&script)) {
return;
}
v8::MaybeLocal<v8::Value> result = script->Run(context);
if (result.IsEmpty()) {
return;
}
}
Napi::Value NodeRuntime::compile_script(const std::string &code, const std::string &filename) {
if (!node_initialized) {
init_once();
}
v8::Local<v8::Context> context = node_context.Get(isolate);
v8::Context::Scope context_scope(context);
// 检测是否为 ESM
v8::Local<v8::Value> result;
if (is_esm_file(filename, code)) {
result = compile_esm_module(code, filename);
} else {
result = compile_cjs_module(code, filename);
}
if (result.IsEmpty()) {
return Napi::Value();
}
// 使用thread_local_env来转换v8值为Napi::Value
if (thread_local_env == nullptr) {
godot::UtilityFunctions::printerr("[compile_script] Error: thread_local_env is not set");
return Napi::Value();
}
return Napi::Value(thread_local_env, reinterpret_cast<napi_value>(*result));
}
bool NodeRuntime::is_esm_file(const std::string &filename, const std::string &code) {
// 1. 检查文件扩展名(最高优先级)
if (filename.size() >= 4 && filename.substr(filename.size() - 4) == ".mjs") {
return true;
}
if (filename.size() >= 4 && filename.substr(filename.size() - 4) == ".cjs") {
return false;
}
// 2. 内容检测:如果有 CommonJS 特征,直接判定为 CJS
if (code.find("module.exports") != std::string::npos ||
code.find("exports.") != std::string::npos ||
code.find("require(") != std::string::npos) {
return false;
}
// 3. 内容检测:如果有 ESM 特征,判定为 ESM
if (code.find("import ") != std::string::npos ||
code.find("export ") != std::string::npos ||
code.find("import{") != std::string::npos ||
code.find("export{") != std::string::npos ||
code.find("export default") != std::string::npos) {
return true;
}
// 4. 对于 .js 文件,检查 package.json 的 "type" 字段
if (filename.size() >= 3 && filename.substr(filename.size() - 3) == ".js") {
std::string dir = filename;
size_t last_slash = dir.find_last_of("/\\");
if (last_slash != std::string::npos) {
dir = dir.substr(0, last_slash);
}
while (!dir.empty()) {
std::string pkg_path = dir + "/package.json";
godot::String gd_pkg_path = godot::String::utf8(pkg_path.c_str());
if (godot::FileAccess::file_exists(gd_pkg_path)) {
godot::Ref<godot::FileAccess> file = godot::FileAccess::open(gd_pkg_path, godot::FileAccess::READ);
if (file.is_valid()) {
godot::String content = file->get_as_text();
godot::Dictionary json = static_cast<godot::Dictionary>(godot::JSON::parse_string(content));
if (json["type"] == "module") {
return true;
} else if (json["type"] == "commonjs") {
return false;
}
}
}
}
}
// 5. 默认为 CommonJS
return false;
}
bool NodeRuntime::is_esm_file() {
godot::String gd_pkg_path = godot::String::utf8("res://package.json");
if (godot::FileAccess::file_exists(gd_pkg_path)) {
godot::Ref<godot::FileAccess> file = godot::FileAccess::open(gd_pkg_path, godot::FileAccess::READ);
if (file.is_valid()) {
godot::String content = file->get_as_text();
godot::Dictionary json = static_cast<godot::Dictionary>(godot::JSON::parse_string(content));
if (static_cast<godot::String>(json["type"]) == "module") {
return true;
} else if (static_cast<godot::String>(json["type"]) == "commonjs") {
return false;
}
}
}
return false;
}
v8::Local<v8::Value> NodeRuntime::compile_esm_module(const std::string &code, const std::string &filename) {
v8::Local<v8::Context> context = node_context.Get(isolate);
v8::Context::Scope context_scope(context);
v8::EscapableHandleScope escapable_scope(isolate);
v8::Local<v8::String> fn_name = v8::String::NewFromUtf8Literal(isolate, "__gode_compile_esm");
v8::Local<v8::Value> fn_val;
if (!context->Global()->Get(context, fn_name).ToLocal(&fn_val) || !fn_val->IsFunction()) {
godot::UtilityFunctions::print("compile_esm_module: __gode_compile_esm not found");
return v8::Local<v8::Value>();
}
v8::Local<v8::Function> fn = fn_val.As<v8::Function>();
v8::Local<v8::Value> args[] = {
v8::String::NewFromUtf8(isolate, code.c_str(), v8::NewStringType::kNormal).ToLocalChecked(),
v8::String::NewFromUtf8(isolate, filename.c_str(), v8::NewStringType::kNormal).ToLocalChecked()
};
v8::MaybeLocal<v8::Value> result = fn->Call(context, context->Global(), 2, args);
if (result.IsEmpty()) {
godot::UtilityFunctions::print("compile_esm_module: fn->Call returned empty");
return v8::Local<v8::Value>();
}
v8::Local<v8::Value> promise_val = result.ToLocalChecked();
// 检查是否是 Promise
if (!promise_val->IsPromise()) {
godot::UtilityFunctions::print("compile_esm_module: result is not a Promise");
return v8::Local<v8::Value>();
}
v8::Local<v8::Promise> promise = promise_val.As<v8::Promise>();
// ESM 加载涉及大量异步操作,循环驱动事件循环直到 Promise settled
while (promise->State() == v8::Promise::kPending) {
isolate->PerformMicrotaskCheckpoint();
uv_run(uv_default_loop(), UV_RUN_ONCE);
if (promise->State() != v8::Promise::kPending) break;
}
if (promise->State() == v8::Promise::kRejected) {
v8::Local<v8::Value> error = promise->Result();
v8::String::Utf8Value error_str(isolate, error);
godot::UtilityFunctions::print("compile_esm_module: Promise rejected: ", *error_str);
return v8::Local<v8::Value>();
}
v8::Local<v8::Value> final_exports = promise->Result();
if (final_exports->IsUndefined()) {
v8::Local<v8::Value> undefined_val = v8::Undefined(isolate);
return escapable_scope.Escape(undefined_val);
}
return escapable_scope.Escape(final_exports);
}
v8::Local<v8::Value> NodeRuntime::compile_cjs_module(const std::string &code, const std::string &filename) {
v8::Local<v8::Context> context = node_context.Get(isolate);
v8::Context::Scope context_scope(context);
v8::EscapableHandleScope escapable_scope(isolate);
v8::Local<v8::String> fn_name = v8::String::NewFromUtf8Literal(isolate, "__gode_compile");
v8::Local<v8::Value> fn_val;
if (!context->Global()->Get(context, fn_name).ToLocal(&fn_val) || !fn_val->IsFunction()) {