Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: compile

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Replaced shared_ptr by boost-like intrusive_ptr Created Jan. 28, 2016, 7:50 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « .hgignore ('k') | compiled/ActiveFilter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2
3 import json
4 import os
5 import re
6 import subprocess
7 import warnings
8
9 EMSCRIPTEN_PATH = '../emscripten'
10 SOURCE_DIR = './compiled'
11 SOURCE_FILES = [
12 os.path.join(SOURCE_DIR, f)
13 for f in os.listdir(SOURCE_DIR)
14 if f.endswith('.cpp')
15 ]
16 API_FILE = os.path.join(SOURCE_DIR, 'api.cpp')
17 API_OUTPUT = os.path.join(SOURCE_DIR, 'api.js')
18 COMPILER_OUTPUT = './lib/compiled.js'
19 GENERATION_PARAMS = {
20 'SHELL_FILE': "'%s'" % os.path.abspath(os.path.join(SOURCE_DIR, 'shell.js')),
21 'TOTAL_MEMORY': 16*1024*1024,
22 'TOTAL_STACK': 1*1024*1024,
23 'ALLOW_MEMORY_GROWTH': 1,
24 'NO_EXIT_RUNTIME': 1,
25 'DISABLE_EXCEPTION_CATCHING': 0,
26 'NO_DYNAMIC_EXECUTION': 1,
27 'NO_BROWSER': 1,
28 'NO_FILESYSTEM': 1,
29 'INVOKE_RUN': 0,
30 'NODE_STDOUT_FLUSH_WORKAROUND': 0,
31 }
32 DEFINES = ['DEBUG']
33 ADDITIONAL_PARAMS = ['-O3', '-m32', '-std=gnu++11', '--memory-init-file', '0',
34 '--emit-symbol-map']
35
36 def getenv():
37 path = []
38 env = {}
39 output = subprocess.check_output([
40 '/bin/sh', '-c', os.path.join(EMSCRIPTEN_PATH, 'emsdk_env.sh')])
41 for line in output.splitlines():
42 match = re.search(r'^\s*PATH\s*\+=\s*(.*)', line)
43 if match:
44 path.append(match.group(1))
45 match = re.search(r'^\s*(\w+)\s*=\s*(.*)', line)
46 if match:
47 env[match.group(1)] = match.group(2)
48 env['PATH'] = ':'.join([os.environ['PATH']] + path)
49 return env
50
51 def generate_api(env):
52 params = [os.path.join(env['EMSCRIPTEN'], 'emcc'), '-E', API_FILE]
53 params.extend(ADDITIONAL_PARAMS)
54 output = subprocess.check_output(params, env=env)
55
56 differentiators = {}
57 differentiator = None
58
59 cls = None
60 method = None
61
62 def wrap_call(func, is_instance, result_type, string_args):
63 prefix = '''\
64 function()
65 {
66 var params = Array.prototype.slice.apply(arguments);
67 '''
68 suffix = '''\
69 return result;
70 }'''
71
72 if result_type == 'string' or len(string_args):
73 prefix += ' var sp = Runtime.stackSave();\n'
74 suffix = ' Runtime.stackRestore(sp);\n' + suffix
75
76 for pos in string_args:
77 prefix += ' params[%i] = createString(params[%i]);\n' % (pos, pos)
78
79 if is_instance:
80 prefix += ' params.unshift(this._pointer);\n'
81
82 if result_type == 'primitive':
83 prefix += ' var result = _%s.apply(null, params);\n' % func
84 elif result_type == 'string':
85 prefix += ' params.unshift(createString());\n'
86 prefix += ' _%s.apply(null, params);\n' % func
87 prefix += ' var result = getStringData(params[0]);\n'
88 else:
89 prefix += ' var pointer = _%s.apply(null, params);\n' % func
90 if result_type in differentiators:
91 prefix += ' var type = _%s(pointer);\n' % differentiator['func']
92 prefix += ' if (type in %s_mapping)\n' % result_type
93 prefix += ' var result = new (exports[%s_mapping[type]])(pointer);\n' % result_type
94 prefix += ' else\n'
95 prefix += ' throw new Error("Unexpected %s type: " + type);\n' % resu lt_type
96 else:
97 prefix += ' var result = %s(pointer);\n' % result_type
98
99 return prefix + suffix
100
101 def property_descriptor(property):
102 if property['type'] == 'static':
103 return 'value: %s' % property['getter']
104
105 result = 'get: %s' % wrap_call(property['getter'], True, property['type'], [ ])
106 if property['setter'] is not None:
107 string_args = [0] if property['type'] == 'string' else []
108 result += ', set: %s' % wrap_call(property['setter'], True, 'primitive', s tring_args)
109 return result
110
111 def method_descriptor(method):
112 return wrap_call(method['func'], method['type'] == 'instance',
113 method['result'], method['string_args'])
114
115 def write_class(file, cls):
116 name = cls['name']
117 if name in differentiators:
118 print >>file, 'var %s_mapping = %s;' % (
119 name, json.dumps(differentiators[name]['mapping'], sort_keys=True))
120
121 print >>file, 'exports.%s = createClass(%s);' % (
122 name, 'exports.' + cls['superclass'] if cls['superclass'] else '')
123 for property in cls['properties']:
124 print >>file, 'Object.defineProperty(exports.%s.prototype, "%s", {%s});' % (
125 name, property['name'], property_descriptor(property))
126 for method in cls['methods']:
127 obj = ('exports.%s' if method['type'] == 'class' else 'exports.%s.prototyp e') % name
128 print >>file, '%s.%s = %s;' % (
129 obj, method['name'], method_descriptor(method))
130 for initializer in cls['class_initializers']:
131 print >>file, '_%s();' % initializer
132
133 def handle_class(name):
134 if cls is not None:
135 write_class(file, cls)
136 differentiator = None
137 return {
138 'name': command[1],
139 'superclass': None,
140 'properties': [],
141 'methods': [],
142 'class_initializers': [],
143 }
144
145 def handle_superclass(name):
146 if cls is None:
147 warnings.warn('Superclass declared outside a class: ' + name)
148 return
149 differentiator = None
150 cls['superclass'] = name
151
152 def handle_class_init(func):
153 if cls is None:
154 warnings.warn('Class initializer declared outside a class: ' + func)
155 return
156 differentiator = None
157 method = None
158 cls['class_initializers'].append(func)
159
160 def handle_differentiator(cls, func):
161 differentiator = {'func': func, 'mapping': {}}
162 differentiators[cls] = differentiator
163 return differentiator
164
165 def handle_differentiator_mapping(value, subclass):
166 if differentiator is None:
167 warnings.warn('Differentiator mapping declared without a differentiator: ' + subclass)
168 return
169 differentiator['mapping'][value] = subclass
170
171 def handle_property(type, name, getter, setter):
172 if cls is None:
173 warnings.warn('Property declared outside a class: ' + name)
174 return
175 method = None
176 differentiator = None
177 cls['properties'].append({
178 'type': type,
179 'name': name,
180 'getter': getter,
181 'setter': setter,
182 })
183
184 def handle_method(type, name, func):
185 if cls is None:
186 warnings.warn('Method declared outside a class: ' + name)
187 return
188 differentiator = None
189 method = {
190 'type': type,
191 'name': name,
192 'func': func,
193 'result': 'primitive',
194 'string_args': [],
195 }
196 cls['methods'].append(method)
197 return method
198
199 def handle_method_result(type):
200 if method is None:
201 warnings.warn('Method result declared without a method definition')
202 return
203 method['result'] = type
204
205 def handle_string_arg(pos):
206 if method is None:
207 warnings.warn('Argument type declared without a method definition')
208 return
209 method['string_args'].append(int(pos))
210
211 with open(API_OUTPUT, 'w') as file:
212 for line in output.splitlines():
213 match = re.search(r'#pragma\s+comment\((.+?)\)', line)
214 if match:
215 command = match.group(1).strip().split()
216 if command[0] == 'class':
217 cls = handle_class(command[1])
218 elif command[0] == 'augments':
219 handle_superclass(command[1])
220 elif command[0] == 'class_init':
221 handle_class_init(command[1])
222 elif command[0] == 'differentiator':
223 differentiator = handle_differentiator(command[1], command[2])
224 elif command[0] == 'differentiator_mapping':
225 handle_differentiator_mapping(command[1], command[2])
226 elif command[0] == 'property':
227 handle_property('primitive', command[1], command[2], command[3] if len (command) > 3 else None)
228 elif command[0] == 'string_property':
229 handle_property('string', command[1], command[2], command[3] if len(co mmand) > 3 else None)
230 elif command[0] == 'static_property':
231 handle_property('static', command[1], command[2], None)
232 elif command[0] == 'method':
233 method = handle_method('instance', command[1], command[2])
234 elif command[0] == 'class_method':
235 method = handle_method('class', command[1], command[2])
236 elif command[0] == 'string_result':
237 handle_method_result('string')
238 elif command[0] == 'pointer_result':
239 handle_method_result(command[1])
240 elif command[0] == 'string_arg':
241 handle_string_arg(command[1])
242 else:
243 warnings.warn('Unknown declaration: ' + str(command))
244
245 if cls is not None:
246 write_class(file, cls)
247
248 def run_compiler(env):
249 params = [
250 os.path.join(env['EMSCRIPTEN'], 'emcc'),
251 '-o', COMPILER_OUTPUT,
252 '--post-js', API_OUTPUT,
253 ]
254 params.extend(SOURCE_FILES)
255 params.extend('-D' + flag for flag in DEFINES)
256 for key, value in GENERATION_PARAMS.iteritems():
257 params.extend(['-s', '%s=%s' % (key, str(value))])
258 params.extend(ADDITIONAL_PARAMS)
259 subprocess.check_call(params, env=env)
260
261 if __name__ == '__main__':
262 env = getenv()
263 generate_api(env)
264 run_compiler(env)
OLDNEW
« no previous file with comments | « .hgignore ('k') | compiled/ActiveFilter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld