8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d24bd20 commit 85a9295Copy full SHA for 85a9295
src/node_javascript.cc
@@ -6,33 +6,46 @@
6
7
namespace node {
8
9
-using v8::HandleScope;
10
using v8::Local;
11
using v8::NewStringType;
12
using v8::Object;
13
using v8::String;
14
+// id##_data is defined in node_natives.h.
15
+#define V(id) \
16
+ static struct : public String::ExternalOneByteStringResource { \
17
+ const char* data() const override { \
18
+ return reinterpret_cast<const char*>(id##_data); \
19
+ } \
20
+ size_t length() const override { return sizeof(id##_data); } \
21
+ void Dispose() override { /* Default calls `delete this`. */ } \
22
+ } id##_external_data;
23
+NODE_NATIVES_MAP(V)
24
+#undef V
25
+
26
Local<String> MainSource(Environment* env) {
- return String::NewFromUtf8(
- env->isolate(),
- reinterpret_cast<const char*>(internal_bootstrap_node_native),
- NewStringType::kNormal,
- sizeof(internal_bootstrap_node_native)).ToLocalChecked();
27
+ auto maybe_string =
28
+ String::NewExternalOneByte(
29
+ env->isolate(),
30
+ &internal_bootstrap_node_external_data);
31
+ return maybe_string.ToLocalChecked();
32
}
33
34
void DefineJavaScript(Environment* env, Local<Object> target) {
- HandleScope scope(env->isolate());
-
- for (auto native : natives) {
- if (native.source != internal_bootstrap_node_native) {
- Local<String> name = String::NewFromUtf8(env->isolate(), native.name);
- Local<String> source =
- String::NewFromUtf8(
- env->isolate(), reinterpret_cast<const char*>(native.source),
- NewStringType::kNormal, native.source_len).ToLocalChecked();
- target->Set(name, source);
- }
35
+ auto context = env->context();
36
37
+ do { \
38
+ auto key = \
39
+ String::NewFromOneByte( \
40
+ env->isolate(), id##_name, NewStringType::kNormal, \
41
+ sizeof(id##_name)).ToLocalChecked(); \
42
+ auto value = \
43
+ String::NewExternalOneByte( \
44
+ env->isolate(), &id##_external_data).ToLocalChecked(); \
45
+ CHECK(target->Set(context, key, value).FromJust()); \
46
+ } while (0);
47
+ NODE_NATIVES_MAP(V)
48
49
50
51
} // namespace node
tools/js2c.py
@@ -37,8 +37,11 @@
import string
-def ToCArray(filename, lines):
- return ','.join(str(ord(c)) for c in lines)
+def ToCString(contents):
+ step = 20
+ slices = (contents[i:i+step] for i in xrange(0, len(contents), step))
+ slices = map(lambda s: ','.join(str(ord(c)) for c in s), slices)
+ return ',\n'.join(slices)
def ReadFile(filename):
@@ -61,21 +64,6 @@ def ReadLines(filename):
61
64
return result
62
65
63
66
-def LoadConfigFrom(name):
- import ConfigParser
- config = ConfigParser.ConfigParser()
67
- config.read(name)
68
- return config
69
70
71
-def ParseValue(string):
72
- string = string.strip()
73
- if string.startswith('[') and string.endswith(']'):
74
- return string.lstrip('[').rstrip(']').split()
75
- else:
76
- return string
77
78
79
def ExpandConstants(lines, constants):
80
for key, value in constants.items():
81
lines = lines.replace(key, str(value))
@@ -174,53 +162,37 @@ def ReadMacros(lines):
174
162
175
163
176
164
HEADER_TEMPLATE = """\
177
-#ifndef node_natives_h
178
-#define node_natives_h
179
-namespace node {
180
181
-%(source_lines)s\
165
+#ifndef NODE_NATIVES_H_
166
+#define NODE_NATIVES_H_
182
167
183
-struct _native {
184
- const char* name;
185
- const unsigned char* source;
186
- size_t source_len;
187
-};
168
+#include <stdint.h>
188
169
189
-static const struct _native natives[] = { %(native_lines)s };
170
+#define NODE_NATIVES_MAP(V) \\
171
+{node_natives_map}
190
172
191
-}
192
-#endif
193
-"""
194
195
196
-NATIVE_DECLARATION = """\
197
- { "%(id)s", %(escaped_id)s_native, sizeof(%(escaped_id)s_native) },
198
173
+namespace node {{
+{sources}
+}} // namespace node
199
200
-SOURCE_DECLARATION = """\
201
- const unsigned char %(escaped_id)s_native[] = { %(data)s };
+#endif // NODE_NATIVES_H_ 9E88
202
"""
203
204
205
-GET_DELAY_INDEX_CASE = """\
206
- if (strcmp(name, "%(id)s") == 0) return %(i)i;
+NODE_NATIVES_MAP = """\
+ V({escaped_id}) \\
207
208
209
210
-GET_DELAY_SCRIPT_SOURCE_CASE = """\
211
- if (index == %(i)i) return Vector<const char>(%(id)s, %(length)i);
+SOURCES = """\
+static const uint8_t {escaped_id}_name[] = {{
+{name}}};
+static const uint8_t {escaped_id}_data[] = {{
+{data}}};
212
213
214
215
-GET_DELAY_SCRIPT_NAME_CASE = """\
216
- if (index == %(i)i) return Vector<const char>("%(name)s", %(length)i);
217
218
219
def JS2C(source, target):
220
- ids = []
221
- delay_ids = []
222
modules = []
223
- # Locate the macros file name.
224
consts = {}
225
macros = {}
226
macro_lines = []
@@ -235,18 +207,14 @@ def JS2C(source, target):
235
(consts, macros) = ReadMacros(macro_lines)
236
237
# Build source code lines
238
- source_lines = [ ]
239
- source_lines_empty = []
240
241
- native_lines = []
+ node_natives_map = []
+ sources = []
242
243
for s in modules:
244
- delay = str(s).endswith('-delay.js')
245
lines = ReadFile(str(s))
246
247
lines = ExpandConstants(lines, consts)
248
lines = ExpandMacros(lines, macros)
249
- data = ToCArray(s, lines)
+ data = ToCString(lines)
250
251
# On Windows, "./foo.bar" in the .gyp file is passed as "foo.bar"
252
# so don't assume there is always a slash in the file path.
@@ -258,89 +226,19 @@ def JS2C(source, target):
258
if '.' in id:
259
227
id = id.split('.', 1)[0]
260
228
261
- if delay: id = id[:-6]
262
- if delay:
263
- delay_ids.append((id, len(lines)))
264
265
- ids.append((id, len(lines)))
266
229
+ name = ToCString(id)
267
230
escaped_id = id.replace('-', '_').replace('/', '_')
268
- source_lines.append(SOURCE_DECLARATION % {
269
- 'id': id,
270
- 'escaped_id': escaped_id,
271
- 'data': data
272
- })
273
- source_lines_empty.append(SOURCE_DECLARATION % {
274
275
276
- 'data': 0
277
278
- native_lines.append(NATIVE_DECLARATION % {
279
280
- 'escaped_id': escaped_id
281
282
283
- # Build delay support functions
284
- get_index_cases = [ ]
285
- get_script_source_cases = [ ]
286
- get_script_name_cases = [ ]
287
288
- i = 0
289
- for (id, length) in delay_ids:
290
- native_name = "native %s.js" % id
291
- get_index_cases.append(GET_DELAY_INDEX_CASE % { 'id': id, 'i': i })
292
- get_script_source_cases.append(GET_DELAY_SCRIPT_SOURCE_CASE % {
293
294
- 'length': length,
295
- 'i': i
296
297
- get_script_name_cases.append(GET_DELAY_SCRIPT_NAME_CASE % {
298
- 'name': native_name,
299
- 'length': len(native_name),
300
301
- });
302
- i = i + 1
303
304
- for (id, length) in ids:
305
306
307
308
309
310
311
312
313
314
315
316
317
231
+ node_natives_map.append(NODE_NATIVES_MAP.format(**locals()))
232
+ sources.append(SOURCES.format(**locals()))
233
234
+ node_natives_map = ''.join(node_natives_map)
+ sources = ''.join(sources)
318
319 D306 code>
# Emit result
320
output = open(str(target[0]), "w")
321
- output.write(HEADER_TEMPLATE % {
322
- 'builtin_count': len(ids) + len(delay_ids),
323
- 'delay_count': len(delay_ids),
324
- 'source_lines': "\n".join(source_lines),
325
- 'native_lines': "\n".join(native_lines),
326
- 'get_index_cases': "".join(get_index_cases),
327
- 'get_script_source_cases': "".join(get_script_source_cases),
328
- 'get_script_name_cases': "".join(get_script_name_cases)
329
+ output.write(HEADER_TEMPLATE.format(**locals()))
330
output.close()
331
332
- if len(target) > 1:
333
- output = open(str(target[1]), "w")
334
335
336
337
- 'source_lines': "\n".join(source_lines_empty),
338
339
340
341
342
- output.close()
343
344
def main():
345
natives = sys.argv[1]
346
source_files = sys.argv[2:]