nodejs / node-chakracore

Node.js on ChakraCore :sparkles::turtle::rocket::sparkles:
Other
1.92k stars 342 forks source link

tools: cleanup js2c #592

Closed kfarnung closed 5 years ago

kfarnung commented 6 years ago

Slimmed down the ChakraCore modifications to js2c to make future merges easier. Re-used existing globals rather than creating new ones.

Checklist
kfarnung commented 6 years ago

Here's a diff with the upstream merge-base:

diff --git a/tools/js2c.py b/tools/js2c.py
index 40f2bc6f48..b1dabfbde1 100755
--- a/tools/js2c.py
+++ b/tools/js2c.py
@@ -174,6 +174,19 @@ def ReadMacros(lines):
           raise Exception("Illegal line: " + line)
   return (constants, macros)

+TEMPLATE_FOR_NAMESPACE = """
+namespace {namespace} {{
+{definitions}
+}} // namespace {namespace}
+"""
+
+ONE_BYTE_STRING_FOR_NAMESPACE = """
+static const uint8_t raw_{var}[] = {{ {data} }};
+"""
+
+TWO_BYTE_STRING_FOR_NAMESPACE = """
+static const uint16_t raw_{var}[] = {{ {data} }};
+"""

 TEMPLATE = """
 #include "node.h"
@@ -273,7 +286,7 @@ def Render(var, data):
   return template.format(var=var, data=data)

-def JS2C(source, target):
+def JS2C(source, target, namespace):
   modules = []
   consts = {}
   macros = {}
@@ -344,17 +357,34 @@ def JS2C(source, target):
   output = open(str(target[0]), "w")
   output.write(TEMPLATE.format(definitions=''.join(definitions),
                                initializers=''.join(initializers),
-                               hash_initializers=''.join(hash_initializers)))
+                               hash_initializers=''.join(hash_initializers),
+                               namespace=namespace))
   output.close()

+
+NAMESPACE_SWITCH = "--namespace="
+
 def main():
-  natives = sys.argv[1]
-  source_files = sys.argv[2:]
+  i = 1
+  namespace = 'node'
+
+  if sys.argv[i].startswith(NAMESPACE_SWITCH):
+    global TEMPLATE
+    global ONE_BYTE_STRING
+    global TWO_BYTE_STRING
+    TEMPLATE = TEMPLATE_FOR_NAMESPACE
+    ONE_BYTE_STRING = ONE_BYTE_STRING_FOR_NAMESPACE
+    TWO_BYTE_STRING = ONE_BYTE_STRING_FOR_NAMESPACE
+    namespace = sys.argv[i][len(NAMESPACE_SWITCH):]
+    i += 1
+
+  natives = sys.argv[i]
+  source_files = sys.argv[(i + 1):]
   if source_files[-2] == '-t':
     global TEMPLATE
     TEMPLATE = source_files[-1]
     source_files = source_files[:-2]
-  JS2C(source_files, [natives])
+  JS2C(source_files, [natives], namespace)

 if __name__ == "__main__":
   main()
kfarnung commented 6 years ago

CI: https://ci.nodejs.org/job/chakracore-test-pull-request/314/