@@ -148,7 +148,6 @@ constexpr std::string_view kJsSuffix = ".js";
148
148
constexpr std::string_view kGypiSuffix = " .gypi" ;
149
149
constexpr std::string_view depsPrefix = " deps/" ;
150
150
constexpr std::string_view libPrefix = " lib/" ;
151
- constexpr std::string_view kVarSuffix = " _raw" ;
152
151
std::set<std::string_view> kAllowedExtensions {
153
152
kGypiSuffix , kJsSuffix , kMjsSuffix };
154
153
@@ -190,6 +189,7 @@ std::vector<char> Join(const Fragments& fragments,
190
189
const char * kTemplate = R"(
191
190
#include "env-inl.h"
192
191
#include "node_builtins.h"
192
+ #include "node_external_reference.h"
193
193
#include "node_internals.h"
194
194
195
195
namespace node {
@@ -209,8 +209,13 @@ void BuiltinLoader::LoadJavaScriptSource() {
209
209
source_ = global_source_map;
210
210
}
211
211
212
+ void RegisterExternalReferencesForInternalizedBuiltinCode(
213
+ ExternalReferenceRegistry* registry) {
214
+ %.*s
215
+ }
216
+
212
217
UnionBytes BuiltinLoader::GetConfig() {
213
- return UnionBytes(config_raw, %zu); // config.gypi
218
+ return UnionBytes(&config_resource);
214
219
}
215
220
216
221
} // namespace builtins
@@ -220,22 +225,16 @@ UnionBytes BuiltinLoader::GetConfig() {
220
225
221
226
Fragment Format (const Fragments& definitions,
222
227
const Fragments& initializers,
223
- size_t config_size) {
224
- // Definitions:
225
- // static const uint8_t fs_raw[] = {
226
- // ....
227
- // };
228
- // static const uint16_t internal_cli_table_raw[] = {
229
- // ....
230
- // };
228
+ const Fragments& registrations) {
231
229
std::vector<char > def_buf = Join (definitions, " \n " );
232
230
size_t def_size = def_buf.size ();
233
- // Initializers of the BuiltinSourceMap:
234
- // {"fs", UnionBytes{fs_raw, 84031}},
235
231
std::vector<char > init_buf = Join (initializers, " \n " );
236
232
size_t init_size = init_buf.size ();
233
+ std::vector<char > reg_buf = Join (registrations, " \n " );
234
+ size_t reg_size = reg_buf.size ();
237
235
238
- size_t result_size = def_size + init_size + strlen (kTemplate ) + 100 ;
236
+ size_t result_size =
237
+ def_size + init_size + reg_size + strlen (kTemplate ) + 100 ;
239
238
std::vector<char > result (result_size, 0 );
240
239
int r = snprintf (result.data (),
241
240
result_size,
@@ -244,7 +243,8 @@ Fragment Format(const Fragments& definitions,
244
243
def_buf.data (),
245
244
static_cast <int >(init_buf.size ()),
246
245
init_buf.data (),
247
- config_size);
246
+ static_cast <int >(reg_buf.size ()),
247
+ reg_buf.data ());
248
248
result.resize (r);
249
249
return result;
250
250
}
@@ -365,21 +365,15 @@ std::string GetFileId(const std::string& filename) {
365
365
}
366
366
367
367
std::string GetVariableName (const std::string& id) {
368
- std::vector<char > var_buf;
369
- size_t length = id.size ();
370
- var_buf.reserve (length + kVarSuffix .size ());
368
+ std::string result = id;
369
+ size_t length = result.size ();
371
370
372
371
for (size_t i = 0 ; i < length; ++i) {
373
- if (id[i] == ' .' || id[i] == ' -' || id[i] == ' /' ) {
374
- var_buf.push_back (' _' );
375
- } else {
376
- var_buf.push_back (id[i]);
372
+ if (result[i] == ' .' || result[i] == ' -' || result[i] == ' /' ) {
373
+ result[i] = ' _' ;
377
374
}
378
375
}
379
- for (size_t i = 0 ; i < kVarSuffix .size (); ++i) {
380
- var_buf.push_back (kVarSuffix [i]);
381
- }
382
- return std::string (var_buf.data (), var_buf.size ());
376
+ return result;
383
377
}
384
378
385
379
std::vector<std::string> GetCodeTable () {
@@ -396,22 +390,41 @@ const std::string& GetCode(uint16_t index) {
396
390
return table[index];
397
391
}
398
392
399
- constexpr std::string_view literal_end = " \n };\n " ;
393
+ // Definitions:
394
+ // static const uint8_t fs_raw[] = {
395
+ // ....
396
+ // };
397
+ //
398
+ // static StaticExternalOneByteResource fs_resource(fs_raw, 1234, nullptr);
399
+ //
400
+ // static const uint16_t internal_cli_table_raw[] = {
401
+ // ....
402
+ // };
403
+ //
404
+ // static StaticExternalTwoByteResource
405
+ // internal_cli_table_resource(internal_cli_table_raw, 1234, nullptr);
406
+ constexpr std::string_view literal_end = " \n };\n\n " ;
400
407
template <typename T>
401
- Fragment ConvertToLiteral (const std::vector<T>& code, const std::string& var) {
408
+ Fragment GetDefinitionImpl (const std::vector<T>& code, const std::string& var) {
402
409
size_t count = code.size ();
403
410
404
411
constexpr bool is_two_byte = std::is_same_v<T, uint16_t >;
405
412
static_assert (is_two_byte || std::is_same_v<T, char >);
406
413
constexpr size_t unit =
407
414
(is_two_byte ? 5 : 3 ) + 1 ; // 0-65536 or 0-127 and a ","
408
- constexpr const char * id = is_two_byte ? " uint16_t" : " uint8_t" ;
415
+ constexpr const char * arr_type = is_two_byte ? " uint16_t" : " uint8_t" ;
416
+ constexpr const char * resource_type = is_two_byte
417
+ ? " StaticExternalTwoByteResource"
418
+ : " StaticExternalOneByteResource" ;
409
419
410
420
size_t def_size = 256 + (count * unit);
411
421
Fragment result (def_size, 0 );
412
422
413
- int cur = snprintf (
414
- result.data (), def_size, " static const %s %s[] = {\n " , id, var.c_str ());
423
+ int cur = snprintf (result.data (),
424
+ def_size,
425
+ " static const %s %s_raw[] = {\n " ,
426
+ arr_type,
427
+ var.c_str ());
415
428
assert (cur != 0 );
416
429
for (size_t i = 0 ; i < count; ++i) {
417
430
// Avoid using snprintf on large chunks of data because it's much slower.
@@ -422,38 +435,43 @@ Fragment ConvertToLiteral(const std::vector<T>& code, const std::string& var) {
422
435
}
423
436
memcpy (result.data () + cur, literal_end.data (), literal_end.size ());
424
437
cur += literal_end.size ();
425
- result.resize (cur);
426
438
439
+ int end_size = snprintf (result.data () + cur,
440
+ result.size () - cur,
441
+ " static %s %s_resource(%s_raw, %zu, nullptr);\n " ,
442
+ resource_type,
443
+ var.c_str (),
444
+ var.c_str (),
445
+ count);
446
+ cur += end_size;
447
+ result.resize (cur);
427
448
return result;
428
449
}
429
450
430
- Fragment GetDefinition (const std::string& var,
431
- const std::vector<char >& code,
432
- size_t * static_size) {
451
+ Fragment GetDefinition (const std::string& var, const std::vector<char >& code) {
433
452
Debug (" GetDefinition %s, code size %zu " , var.c_str (), code.size ());
434
453
bool is_one_byte = simdutf::validate_ascii (code.data (), code.size ());
435
454
Debug (" with %s\n " , is_one_byte ? " 1-byte chars" : " 2-byte chars" );
436
455
437
456
if (is_one_byte) {
438
- *static_size = code.size ();
439
- Debug (" static size %zu\n " , *static_size);
440
- return ConvertToLiteral (code, var);
457
+ Debug (" static size %zu\n " , code.size ());
458
+ return GetDefinitionImpl (code, var);
441
459
} else {
442
460
size_t length = simdutf::utf16_length_from_utf8 (code.data (), code.size ());
443
461
std::vector<uint16_t > utf16 (length);
444
462
size_t utf16_count = simdutf::convert_utf8_to_utf16 (
445
463
code.data (), code.size (), reinterpret_cast <char16_t *>(utf16.data ()));
446
464
assert (utf16_count != 0 );
447
465
utf16.resize (utf16_count);
448
- *static_size = utf16_count;
449
- Debug (" static size %zu\n " , *static_size);
450
- return ConvertToLiteral (utf16, var);
466
+ Debug (" static size %zu\n " , utf16_count);
467
+ return GetDefinitionImpl (utf16, var);
451
468
}
452
469
}
453
470
454
471
int AddModule (const std::string& filename,
455
472
Fragments* definitions,
456
- Fragments* initializers) {
473
+ Fragments* initializers,
474
+ Fragments* registrations) {
457
475
Debug (" AddModule %s start\n " , filename.c_str ());
458
476
459
477
int error = 0 ;
@@ -465,21 +483,29 @@ int AddModule(const std::string& filename,
465
483
if (error != 0 ) {
466
484
return error;
467
485
}
468
- std::string id = GetFileId (filename);
469
- std::string var = GetVariableName (id );
486
+ std::string file_id = GetFileId (filename);
487
+ std::string var = GetVariableName (file_id );
470
488
471
- size_t static_size;
472
- definitions->emplace_back (GetDefinition (var, code, &static_size));
473
-
474
- Fragment& buf = initializers->emplace_back (Fragment (256 , 0 ));
475
- int r = snprintf (buf.data (),
476
- buf.size (),
477
- " {\" %s\" , UnionBytes{%s, %zu} }," ,
478
- id.c_str (),
479
- var.c_str (),
480
- static_size);
481
- buf.resize (r);
489
+ definitions->emplace_back (GetDefinition (var, code));
482
490
491
+ // Initializers of the BuiltinSourceMap:
492
+ // {"fs", UnionBytes{&fs_resource}},
493
+ Fragment& init_buf = initializers->emplace_back (Fragment (256 , 0 ));
494
+ int init_size = snprintf (init_buf.data (),
495
+ init_buf.size (),
496
+ " {\" %s\" , UnionBytes(&%s_resource) }," ,
497
+ file_id.c_str (),
498
+ var.c_str ());
499
+ init_buf.resize (init_size);
500
+
501
+ // Registrations:
502
+ // registry->Register(&fs_resource);
503
+ Fragment& reg_buf = registrations->emplace_back (Fragment (256 , 0 ));
504
+ int reg_size = snprintf (reg_buf.data (),
505
+ reg_buf.size (),
506
+ " registry->Register(&%s_resource);" ,
507
+ var.c_str ());
508
+ reg_buf.resize (reg_size);
483
509
return 0 ;
484
510
}
485
511
@@ -575,10 +601,9 @@ std::vector<char> JSONify(const std::vector<char>& code) {
575
601
return result4;
576
602
}
577
603
578
- int AddGypi (const std::string& id ,
604
+ int AddGypi (const std::string& var ,
579
605
const std::string& filename,
580
- Fragments* definitions,
581
- size_t * config_size) {
606
+ Fragments* definitions) {
582
607
Debug (" AddGypi %s start\n " , filename.c_str ());
583
608
584
609
int error = 0 ;
@@ -590,10 +615,10 @@ int AddGypi(const std::string& id,
590
615
if (error != 0 ) {
591
616
return error;
592
617
}
593
- assert (id == " config_raw " );
618
+ assert (var == " config " );
594
619
595
620
std::vector<char > transformed = JSONify (code);
596
- definitions->emplace_back (GetDefinition (id , transformed, config_size ));
621
+ definitions->emplace_back (GetDefinition (var , transformed));
597
622
return 0 ;
598
623
}
599
624
@@ -605,28 +630,29 @@ int JS2C(const FileList& js_files,
605
630
defintions.reserve (js_files.size () + mjs_files.size () + 1 );
606
631
Fragments initializers;
607
632
initializers.reserve (js_files.size () + mjs_files.size ());
633
+ Fragments registrations;
634
+ registrations.reserve (js_files.size () + mjs_files.size () + 1 );
608
635
609
636
for (const auto & filename : js_files) {
610
- int r = AddModule (filename, &defintions, &initializers);
637
+ int r = AddModule (filename, &defintions, &initializers, ®istrations );
611
638
if (r != 0 ) {
612
639
return r;
613
640
}
614
641
}
615
642
for (const auto & filename : mjs_files) {
616
- int r = AddModule (filename, &defintions, &initializers);
643
+ int r = AddModule (filename, &defintions, &initializers, ®istrations );
617
644
if (r != 0 ) {
618
645
return r;
619
646
}
620
647
}
621
648
622
- size_t config_size = 0 ;
623
649
assert (config == " config.gypi" );
624
650
// "config.gypi" -> config_raw.
625
- int r = AddGypi (" config_raw " , config, &defintions, &config_size );
651
+ int r = AddGypi (" config " , config, &defintions);
626
652
if (r != 0 ) {
627
653
return r;
628
654
}
629
- Fragment out = Format (defintions, initializers, config_size );
655
+ Fragment out = Format (defintions, initializers, registrations );
630
656
return WriteIfChanged (out, dest);
631
657
}
632
658
0 commit comments