Skip to content

Commit

Permalink
feat(js): port and modernize old path module from nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
michaldziuba03 committed Aug 7, 2024
1 parent 9d309cf commit 0132489
Show file tree
Hide file tree
Showing 6 changed files with 666 additions and 23 deletions.
11 changes: 6 additions & 5 deletions codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@ def generate_cpp_header(folder_path, output_file):
f.write(f"#define {header_guard}\n\n")
f.write("#include <unordered_map>\n")
f.write("#include <string>\n\n")
f.write("std::unordered_map<std::string, std::string> js_internals = {\n")
f.write("std::unordered_map<std::string, std::vector<unsigned char>> js_internals = {\n")

first = True
for file_name in os.listdir(folder_path):
if file_name.endswith(".js"):
module_name = os.path.splitext(file_name)[0]
file_path = os.path.join(folder_path, file_name)

with open(file_path, 'r', encoding='utf-8') as js_file:
source_code = js_file.read().replace('\n', '\\n').replace('"', '\\"')
with open(file_path, 'rb') as js_file: # Open file in binary mode
byte_content = js_file.read()
hex_content = ', '.join(f'0x{byte:02x}' for byte in byte_content)

if not first:
f.write(",\n")
first = False

f.write(f' {{"std:{module_name}", "{source_code}"}}')
f.write(f' {{"std:{module_name}", {{ {hex_content} }}}}')

f.write("\n};\n\n")
f.write(f"#endif // {header_guard}\n")

folder_path = "./js"
output_file = "core/js_internals.hh"

generate_cpp_header(folder_path, output_file)
generate_cpp_header(folder_path, output_file)
9 changes: 5 additions & 4 deletions core/js_internals.hh

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ namespace runtime
exit(1);
}

return internal_src->second;
return std::string(internal_src->second.begin(), internal_src->second.end());
}

std::ifstream file(path);
Expand Down
Loading

0 comments on commit 0132489

Please sign in to comment.