Skip to content

Commit c40640c

Browse files
committed
fixup! tools: port js2c.py to C++
1 parent b486521 commit c40640c

File tree

1 file changed

+90
-64
lines changed

1 file changed

+90
-64
lines changed

tools/js2c.cc

Lines changed: 90 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ constexpr std::string_view kJsSuffix = ".js";
148148
constexpr std::string_view kGypiSuffix = ".gypi";
149149
constexpr std::string_view depsPrefix = "deps/";
150150
constexpr std::string_view libPrefix = "lib/";
151-
constexpr std::string_view kVarSuffix = "_raw";
152151
std::set<std::string_view> kAllowedExtensions{
153152
kGypiSuffix, kJsSuffix, kMjsSuffix};
154153

@@ -190,6 +189,7 @@ std::vector<char> Join(const Fragments& fragments,
190189
const char* kTemplate = R"(
191190
#include "env-inl.h"
192191
#include "node_builtins.h"
192+
#include "node_external_reference.h"
193193
#include "node_internals.h"
194194
195195
namespace node {
@@ -209,8 +209,13 @@ void BuiltinLoader::LoadJavaScriptSource() {
209209
source_ = global_source_map;
210210
}
211211
212+
void RegisterExternalReferencesForInternalizedBuiltinCode(
213+
ExternalReferenceRegistry* registry) {
214+
%.*s
215+
}
216+
212217
UnionBytes BuiltinLoader::GetConfig() {
213-
return UnionBytes(config_raw, %zu); // config.gypi
218+
return UnionBytes(&config_resource);
214219
}
215220
216221
} // namespace builtins
@@ -220,22 +225,16 @@ UnionBytes BuiltinLoader::GetConfig() {
220225

221226
Fragment Format(const Fragments& definitions,
222227
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) {
231229
std::vector<char> def_buf = Join(definitions, "\n");
232230
size_t def_size = def_buf.size();
233-
// Initializers of the BuiltinSourceMap:
234-
// {"fs", UnionBytes{fs_raw, 84031}},
235231
std::vector<char> init_buf = Join(initializers, "\n");
236232
size_t init_size = init_buf.size();
233+
std::vector<char> reg_buf = Join(registrations, "\n");
234+
size_t reg_size = reg_buf.size();
237235

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;
239238
std::vector<char> result(result_size, 0);
240239
int r = snprintf(result.data(),
241240
result_size,
@@ -244,7 +243,8 @@ Fragment Format(const Fragments& definitions,
244243
def_buf.data(),
245244
static_cast<int>(init_buf.size()),
246245
init_buf.data(),
247-
config_size);
246+
static_cast<int>(reg_buf.size()),
247+
reg_buf.data());
248248
result.resize(r);
249249
return result;
250250
}
@@ -365,21 +365,15 @@ std::string GetFileId(const std::string& filename) {
365365
}
366366

367367
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();
371370

372371
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] = '_';
377374
}
378375
}
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;
383377
}
384378

385379
std::vector<std::string> GetCodeTable() {
@@ -396,22 +390,41 @@ const std::string& GetCode(uint16_t index) {
396390
return table[index];
397391
}
398392

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";
400407
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) {
402409
size_t count = code.size();
403410

404411
constexpr bool is_two_byte = std::is_same_v<T, uint16_t>;
405412
static_assert(is_two_byte || std::is_same_v<T, char>);
406413
constexpr size_t unit =
407414
(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";
409419

410420
size_t def_size = 256 + (count * unit);
411421
Fragment result(def_size, 0);
412422

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());
415428
assert(cur != 0);
416429
for (size_t i = 0; i < count; ++i) {
417430
// 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) {
422435
}
423436
memcpy(result.data() + cur, literal_end.data(), literal_end.size());
424437
cur += literal_end.size();
425-
result.resize(cur);
426438

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);
427448
return result;
428449
}
429450

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) {
433452
Debug("GetDefinition %s, code size %zu ", var.c_str(), code.size());
434453
bool is_one_byte = simdutf::validate_ascii(code.data(), code.size());
435454
Debug("with %s\n", is_one_byte ? "1-byte chars" : "2-byte chars");
436455

437456
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);
441459
} else {
442460
size_t length = simdutf::utf16_length_from_utf8(code.data(), code.size());
443461
std::vector<uint16_t> utf16(length);
444462
size_t utf16_count = simdutf::convert_utf8_to_utf16(
445463
code.data(), code.size(), reinterpret_cast<char16_t*>(utf16.data()));
446464
assert(utf16_count != 0);
447465
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);
451468
}
452469
}
453470

454471
int AddModule(const std::string& filename,
455472
Fragments* definitions,
456-
Fragments* initializers) {
473+
Fragments* initializers,
474+
Fragments* registrations) {
457475
Debug("AddModule %s start\n", filename.c_str());
458476

459477
int error = 0;
@@ -465,21 +483,29 @@ int AddModule(const std::string& filename,
465483
if (error != 0) {
466484
return error;
467485
}
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);
470488

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));
482490

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);
483509
return 0;
484510
}
485511

@@ -575,10 +601,9 @@ std::vector<char> JSONify(const std::vector<char>& code) {
575601
return result4;
576602
}
577603

578-
int AddGypi(const std::string& id,
604+
int AddGypi(const std::string& var,
579605
const std::string& filename,
580-
Fragments* definitions,
581-
size_t* config_size) {
606+
Fragments* definitions) {
582607
Debug("AddGypi %s start\n", filename.c_str());
583608

584609
int error = 0;
@@ -590,10 +615,10 @@ int AddGypi(const std::string& id,
590615
if (error != 0) {
591616
return error;
592617
}
593-
assert(id == "config_raw");
618+
assert(var == "config");
594619

595620
std::vector<char> transformed = JSONify(code);
596-
definitions->emplace_back(GetDefinition(id, transformed, config_size));
621+
definitions->emplace_back(GetDefinition(var, transformed));
597622
return 0;
598623
}
599624

@@ -605,28 +630,29 @@ int JS2C(const FileList& js_files,
605630
defintions.reserve(js_files.size() + mjs_files.size() + 1);
606631
Fragments initializers;
607632
initializers.reserve(js_files.size() + mjs_files.size());
633+
Fragments registrations;
634+
registrations.reserve(js_files.size() + mjs_files.size() + 1);
608635

609636
for (const auto& filename : js_files) {
610-
int r = AddModule(filename, &defintions, &initializers);
637+
int r = AddModule(filename, &defintions, &initializers, &registrations);
611638
if (r != 0) {
612639
return r;
613640
}
614641
}
615642
for (const auto& filename : mjs_files) {
616-
int r = AddModule(filename, &defintions, &initializers);
643+
int r = AddModule(filename, &defintions, &initializers, &registrations);
617644
if (r != 0) {
618645
return r;
619646
}
620647
}
621648

622-
size_t config_size = 0;
623649
assert(config == "config.gypi");
624650
// "config.gypi" -> config_raw.
625-
int r = AddGypi("config_raw", config, &defintions, &config_size);
651+
int r = AddGypi("config", config, &defintions);
626652
if (r != 0) {
627653
return r;
628654
}
629-
Fragment out = Format(defintions, initializers, config_size);
655+
Fragment out = Format(defintions, initializers, registrations);
630656
return WriteIfChanged(out, dest);
631657
}
632658

0 commit comments

Comments
 (0)