-
Notifications
You must be signed in to change notification settings - Fork 15k
/
build_modify_js2c_py_to_allow_injection_of_original-fs_and_custom_embedder_js.patch
147 lines (125 loc) · 5.93 KB
/
build_modify_js2c_py_to_allow_injection_of_original-fs_and_custom_embedder_js.patch
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
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Cheng Zhao <zcbenz@gmail.com>
Date: Thu, 11 Apr 2019 17:16:13 +0900
Subject: build: modify js2c.py to allow injection of original-fs and custom
embedder JS
This patch does two things:
* Updates js2c.py so that original-fs is automatically created with
support for streams by copying the file at build time and making a new
builtin called "original-fs" and "original-fs/streams"
* Updates js2c.py so that //electron can call it as part of its build
process and provide embedder modules (electrons
renderer/browser/worker/sandboxed bootstrap scripts). These are loaded
through LoadEmbedderJavaScriptSource()
diff --git a/src/node_native_module.cc b/src/node_native_module.cc
index 814adb620dcf8a459bbede11714e9ccd7f4e0b3e..04a706d15f2ab682f8d4ae972abee8986494f7a7 100644
--- a/src/node_native_module.cc
+++ b/src/node_native_module.cc
@@ -23,6 +23,7 @@ NativeModuleLoader NativeModuleLoader::instance_;
NativeModuleLoader::NativeModuleLoader() : config_(GetConfig()) {
LoadJavaScriptSource();
+ LoadEmbedderJavaScriptSource();
}
NativeModuleLoader* NativeModuleLoader::GetInstance() {
diff --git a/src/node_native_module.h b/src/node_native_module.h
index fabaea75686161f488a03349e07049a513b98fad..5a6b01dc12fb77d5f8c26a1153ead2a1b2a8d061 100644
--- a/src/node_native_module.h
+++ b/src/node_native_module.h
@@ -44,6 +44,7 @@ class NativeModuleLoader {
// Generated by tools/js2c.py as node_javascript.cc
void LoadJavaScriptSource(); // Loads data into source_
+ void LoadEmbedderJavaScriptSource(); // Loads embedder data into source_
UnionBytes GetConfig(); // Return data for config.gypi
bool Exists(const char* id);
diff --git a/tools/js2c.py b/tools/js2c.py
index 752344d68c3f63b4c5e491b33d4576ed48f8b74f..a6f0805048e3c3f4dd81ce6e90b684c48e52b67c 100755
--- a/tools/js2c.py
+++ b/tools/js2c.py
@@ -187,13 +187,15 @@ namespace native_module {{
{0}
-void NativeModuleLoader::LoadJavaScriptSource() {{
+void NativeModuleLoader::Load{4}JavaScriptSource() {{
{1}
}}
+#if {2}
UnionBytes NativeModuleLoader::GetConfig() {{
- return UnionBytes(config_raw, {2}); // config.gypi
+ return UnionBytes(config_raw, {3}); // config.gypi
}}
+#endif
}} // namespace native_module
@@ -256,7 +258,7 @@ def AddModule(filename, definitions, initializers):
initializers.append(initializer)
def NormalizeFileName(filename):
- split = filename.split(os.path.sep)
+ split = os.path.normpath(filename).split(os.path.sep)
if split[0] == 'deps':
split = ['internal'] + split
else: # `lib/**/*.js` so drop the 'lib' part
@@ -114,8 +114,8 @@ def GetDefinition(var, source, step=30):
return definition, len(code_points)
-def AddModule(filename, definitions, initializers):
- code = ReadFile(filename)
+def AddModule(filename, definitions, initializers, ReadFileFn=ReadFile):
+ code = ReadFileFn(filename)
name = NormalizeFileName(filename)
slug = SLUGGER_RE.sub('_', name)
var = slug + '_raw'
@@ -141,7 +141,7 @@ def NormalizeFileName(filename):
return os.path.splitext(filename)[0]
-def JS2C(source_files, target):
+def JS2C(source_files, target, only_js):
# Build source code lines
definitions = []
initializers = []
@@ -149,13 +149,26 @@ def JS2C(source_files, target):
for filename in source_files['.js']:
AddModule(filename, definitions, initializers)
- config_def, config_size = handle_config_gypi(source_files['config.gypi'])
- definitions.append(config_def)
+ # Electron: Expose fs module without asar support.
+ if filename == 'lib/fs.js':
+ # Node's 'fs' and 'internal/fs/streams' have a lazy-loaded circular
+ # dependency. So to expose the unmodified Node 'fs' functionality here,
+ # we have to copy both 'fs' *and* 'internal/fs/streams' and modify the
+ # copies to depend on each other instead of on our asarified 'fs' code.
+ # See https://github.com/electron/electron/pull/16028 for more.
+ AddModule('lib/original-fs.js', definitions, initializers, lambda _: ReadFile(filename).replace("require('internal/fs/streams')", "require('internal/original-fs/streams')"))
+ elif filename == 'lib/internal/fs/streams.js':
+ AddModule('lib/internal/original-fs/streams.js', definitions, initializers, lambda _: ReadFile(filename).replace("require('fs')", "require('original-fs')"))
+
+ config_size = 0
+ if not only_js:
+ config_def, config_size = handle_config_gypi(source_files['config.gypi'])
+ definitions.append(config_def)
# Emit result
definitions = ''.join(definitions)
initializers = '\n '.join(initializers)
- out = TEMPLATE.format(definitions, initializers, config_size)
+ out = TEMPLATE.format(definitions, initializers, '0' if only_js else '1', config_size, 'Embedder' if only_js else '')
write_if_chaged(out, target)
@@ -344,17 +358,21 @@ def main():
)
parser.add_argument('--target', help='output file')
parser.add_argument('--verbose', action='store_true', help='output file')
+ parser.add_argument('--only-js', action='store_true', help='do not require or parse any config.gypi files')
parser.add_argument('sources', nargs='*', help='input files')
options = parser.parse_args()
global is_verbose
is_verbose = options.verbose
source_files = functools.reduce(SourceFileByExt, options.sources, {})
# Should have exactly 2 types: `.js`, and `.gypi`
- assert len(source_files) == 2
- # Currently config.gypi is the only `.gypi` file allowed
- assert source_files['.gypi'] == ['config.gypi']
- source_files['config.gypi'] = source_files.pop('.gypi')[0]
- JS2C(source_files, options.target)
+ if options.only_js:
+ assert len(source_files) == 1
+ else:
+ assert len(source_files) == 2
+ # Currently config.gypi is the only `.gypi` file allowed
+ assert source_files['.gypi'][0].endswith('config.gypi')
+ source_files['config.gypi'] = source_files.pop('.gypi')[0]
+ JS2C(source_files, options.target, options.only_js)
if __name__ == "__main__":