Skip to content

Commit

Permalink
Add script to generate builtin class api description from xml doc files
Browse files Browse the repository at this point in the history
  • Loading branch information
Geequlim committed Feb 20, 2019
1 parent 3257b5b commit 06523d1
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 97 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*.bc
*.os
*.gen.cpp
*.gen.json
92 changes: 0 additions & 92 deletions buitin_api.json

This file was deleted.

17 changes: 12 additions & 5 deletions duktape/builtin_binding_generator.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/usr/bin/env python
import json

variant_types = {
"boolean": "Variant::BOOL",
"number": "Variant::REAL",
"string": "Variant::STRING",
"Vector2": "Variant::VECTOR2",
"Vector3": "Variant::VECTOR3",
"Color": "Variant::COLOR",
}

def apply_parttern(template, values):
Expand Down Expand Up @@ -57,8 +61,7 @@ def process_method(cls, method):
"return": {
"void": "return DUK_NO_RET_VAL;",
"this": 'duk_push_this(ctx);',
"Vector2": 'duk_push_variant(ctx, ret);',
"Vector3": 'duk_push_variant(ctx, ret);',
"Variant": 'duk_push_variant(ctx, ret);',
}
}

Expand All @@ -82,7 +85,9 @@ def process_method(cls, method):
duk_put_prop_literal(ctx, -2, "${name}");
'''

return_content = sub_tempate['return'][method['return']];
return_content = sub_tempate['return']['Variant']
if method['return'] in sub_tempate['return']:
return_content = sub_tempate['return'][method['return']];
if method['return'] != 'void':
return_content += '\n return DUK_HAS_RET_VAL;'

Expand Down Expand Up @@ -160,7 +165,7 @@ def generate_binding_code():
}
'''
classes = json.load(
open('E:/Develop/godot/modules/ECMAScript/buitin_api.json', 'r', encoding='utf8')
open('buitin_api.gen.json', 'r', encoding='utf8')
)
class_properties_funcs = ""
class_properties_func_calls = ""
Expand All @@ -177,4 +182,6 @@ def generate_binding_code():
return file_content

if __name__ == "__main__":
print(generate_binding_code())
file = open('duktape/duktape_builtin_bindings.gen.cpp', 'w')
file.write(generate_binding_code())
print('Done')
90 changes: 90 additions & 0 deletions generate_builtin_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env python
import json, os
import xml.etree.ElementTree as ET

DOCS_DIR = "../../godot/doc/classes"

BUILTIN_CLASSES = [
'Vector2',
# 'Vector3',
# 'Color',
]

TYPE_MAP = {
'int': 'number',
'float': 'number',
'bool': 'boolean',
'String': 'string',
}

def parse_class(cls):
ret = {'name': cls.get('name')}
members = []
methods = []
constants = []
ret['properties'] = members
ret['methods'] = methods
ret['constants'] = constants

for m in (cls.find("members") if cls.find("members") is not None else []):
m_dict = dict(m.attrib)
type = m_dict['type']
if type in TYPE_MAP:
type = TYPE_MAP[type]
members.append({'name': m_dict['name'], 'type': type })

for m in (cls.find("methods") if cls.find("methods") is not None else []):
m_dict = dict(m.attrib)
if m_dict['name'] == cls.get('name'):
continue
return_type = m.find("return").attrib["type"]
if return_type in TYPE_MAP:
return_type = TYPE_MAP[return_type]
arguments = []
for arg in m.iter('argument'):
dictArg = dict(arg.attrib)
if "dictArg" in dictArg: dictArg.pop("index")
dictArg["default_value"] = dictArg["default"] if "default" in dictArg else None
if "default" in dictArg: dictArg.pop("default")
type = dictArg['type']
if type in TYPE_MAP:
type = TYPE_MAP[type]
arguments.append({
'type': type,
'default_value': dictArg['default_value'],
'has_default_value': "default" in dictArg
})
methods.append({
'name': m_dict['name'],
'native_method': m_dict['name'],
'return': return_type,
'arguments': arguments,
})
# add equals method
methods.append({
"arguments": [
{
"default_value": None,
"has_default_value": False,
"type": cls.get('name')
}
],
"name": "equals",
"native_method": "operator==",
"return": "boolean"
})
for c in (cls.find("constants") if cls.find("constants") is not None else []):
constants.append(dict(c.attrib))
return ret

def generate_api_json():
classes = []
for cls in BUILTIN_CLASSES:
tree = ET.parse(open(os.path.join(DOCS_DIR, cls + '.xml'), 'r'))
data = tree.getroot()
classes.append(parse_class(data))
json.dump(classes, open('buitin_api.gen.json', 'w', encoding='utf8'), ensure_ascii=False, indent=2, sort_keys=True)
print('Done')

if __name__ == "__main__":
generate_api_json()

0 comments on commit 06523d1

Please sign in to comment.