]>
Commit | Line | Data |
---|---|---|
54256267 PH |
1 | from __future__ import unicode_literals |
2 | ||
3 | import collections | |
4 | import io | |
54256267 PH |
5 | import zlib |
6 | ||
dab0daee YCH |
7 | from .compat import ( |
8 | compat_str, | |
edaa23f8 | 9 | compat_struct_unpack, |
dab0daee | 10 | ) |
70f767dc | 11 | from .utils import ( |
70f767dc PH |
12 | ExtractorError, |
13 | ) | |
54256267 PH |
14 | |
15 | ||
0cb20563 PH |
16 | def _extract_tags(file_contents): |
17 | if file_contents[1:3] != b'WS': | |
18 | raise ExtractorError( | |
19 | 'Not an SWF file; header is %r' % file_contents[:3]) | |
20 | if file_contents[:1] == b'C': | |
21 | content = zlib.decompress(file_contents[8:]) | |
22 | else: | |
23 | raise NotImplementedError( | |
24 | 'Unsupported compression format %r' % | |
25 | file_contents[:1]) | |
26 | ||
27 | # Determine number of bits in framesize rectangle | |
edaa23f8 | 28 | framesize_nbits = compat_struct_unpack('!B', content[:1])[0] >> 3 |
0cb20563 PH |
29 | framesize_len = (5 + 4 * framesize_nbits + 7) // 8 |
30 | ||
31 | pos = framesize_len + 2 + 2 | |
54256267 | 32 | while pos < len(content): |
edaa23f8 | 33 | header16 = compat_struct_unpack('<H', content[pos:pos + 2])[0] |
54256267 PH |
34 | pos += 2 |
35 | tag_code = header16 >> 6 | |
36 | tag_len = header16 & 0x3f | |
37 | if tag_len == 0x3f: | |
edaa23f8 | 38 | tag_len = compat_struct_unpack('<I', content[pos:pos + 4])[0] |
54256267 | 39 | pos += 4 |
0cb20563 PH |
40 | assert pos + tag_len <= len(content), \ |
41 | ('Tag %d ends at %d+%d - that\'s longer than the file (%d)' | |
42 | % (tag_code, pos, tag_len, len(content))) | |
54256267 PH |
43 | yield (tag_code, content[pos:pos + tag_len]) |
44 | pos += tag_len | |
45 | ||
46 | ||
47 | class _AVMClass_Object(object): | |
48 | def __init__(self, avm_class): | |
49 | self.avm_class = avm_class | |
50 | ||
51 | def __repr__(self): | |
52 | return '%s#%x' % (self.avm_class.name, id(self)) | |
53 | ||
54 | ||
0d989011 PH |
55 | class _ScopeDict(dict): |
56 | def __init__(self, avm_class): | |
57 | super(_ScopeDict, self).__init__() | |
58 | self.avm_class = avm_class | |
59 | ||
60 | def __repr__(self): | |
61 | return '%s__Scope(%s)' % ( | |
62 | self.avm_class.name, | |
63 | super(_ScopeDict, self).__repr__()) | |
64 | ||
65 | ||
54256267 | 66 | class _AVMClass(object): |
fbf94a78 | 67 | def __init__(self, name_idx, name, static_properties=None): |
54256267 PH |
68 | self.name_idx = name_idx |
69 | self.name = name | |
70 | self.method_names = {} | |
71 | self.method_idxs = {} | |
72 | self.methods = {} | |
73 | self.method_pyfunctions = {} | |
fbf94a78 | 74 | self.static_properties = static_properties if static_properties else {} |
70f767dc | 75 | |
0d989011 | 76 | self.variables = _ScopeDict(self) |
cd9ad1d7 | 77 | self.constants = {} |
54256267 PH |
78 | |
79 | def make_object(self): | |
80 | return _AVMClass_Object(self) | |
81 | ||
01b4b745 PH |
82 | def __repr__(self): |
83 | return '_AVMClass(%s)' % (self.name) | |
84 | ||
85 | def register_methods(self, methods): | |
86 | self.method_names.update(methods.items()) | |
87 | self.method_idxs.update(dict( | |
88 | (idx, name) | |
89 | for name, idx in methods.items())) | |
90 | ||
54256267 | 91 | |
decf2ae4 PH |
92 | class _Multiname(object): |
93 | def __init__(self, kind): | |
94 | self.kind = kind | |
95 | ||
96 | def __repr__(self): | |
97 | return '[MULTINAME kind: 0x%x]' % self.kind | |
98 | ||
99 | ||
54256267 PH |
100 | def _read_int(reader): |
101 | res = 0 | |
102 | shift = 0 | |
103 | for _ in range(5): | |
104 | buf = reader.read(1) | |
105 | assert len(buf) == 1 | |
edaa23f8 | 106 | b = compat_struct_unpack('<B', buf)[0] |
54256267 PH |
107 | res = res | ((b & 0x7f) << shift) |
108 | if b & 0x80 == 0: | |
109 | break | |
110 | shift += 7 | |
111 | return res | |
112 | ||
113 | ||
114 | def _u30(reader): | |
115 | res = _read_int(reader) | |
116 | assert res & 0xf0000000 == 0 | |
117 | return res | |
351f3738 | 118 | _u32 = _read_int |
54256267 PH |
119 | |
120 | ||
121 | def _s32(reader): | |
122 | v = _read_int(reader) | |
123 | if v & 0x80000000 != 0: | |
124 | v = - ((v ^ 0xffffffff) + 1) | |
125 | return v | |
126 | ||
127 | ||
128 | def _s24(reader): | |
129 | bs = reader.read(3) | |
130 | assert len(bs) == 3 | |
e75c24e8 | 131 | last_byte = b'\xff' if (ord(bs[2:3]) >= 0x80) else b'\x00' |
edaa23f8 | 132 | return compat_struct_unpack('<i', bs + last_byte)[0] |
54256267 PH |
133 | |
134 | ||
135 | def _read_string(reader): | |
136 | slen = _u30(reader) | |
137 | resb = reader.read(slen) | |
138 | assert len(resb) == slen | |
139 | return resb.decode('utf-8') | |
140 | ||
141 | ||
142 | def _read_bytes(count, reader): | |
0cb20563 | 143 | assert count >= 0 |
54256267 PH |
144 | resb = reader.read(count) |
145 | assert len(resb) == count | |
146 | return resb | |
147 | ||
148 | ||
149 | def _read_byte(reader): | |
150 | resb = _read_bytes(1, reader=reader) | |
edaa23f8 | 151 | res = compat_struct_unpack('<B', resb)[0] |
54256267 PH |
152 | return res |
153 | ||
154 | ||
3cbcff8a | 155 | StringClass = _AVMClass('(no name idx)', 'String') |
6b592d93 | 156 | ByteArrayClass = _AVMClass('(no name idx)', 'ByteArray') |
fbf94a78 PH |
157 | TimerClass = _AVMClass('(no name idx)', 'Timer') |
158 | TimerEventClass = _AVMClass('(no name idx)', 'TimerEvent', {'TIMER': 'timer'}) | |
6b592d93 PH |
159 | _builtin_classes = { |
160 | StringClass.name: StringClass, | |
161 | ByteArrayClass.name: ByteArrayClass, | |
fbf94a78 PH |
162 | TimerClass.name: TimerClass, |
163 | TimerEventClass.name: TimerEventClass, | |
6b592d93 | 164 | } |
3cbcff8a PH |
165 | |
166 | ||
4686ae4b | 167 | class _Undefined(object): |
fbf94a78 | 168 | def __bool__(self): |
4686ae4b | 169 | return False |
fbf94a78 | 170 | __nonzero__ = __bool__ |
4686ae4b PH |
171 | |
172 | def __hash__(self): | |
173 | return 0 | |
174 | ||
f48d3e9b PH |
175 | def __str__(self): |
176 | return 'undefined' | |
177 | __repr__ = __str__ | |
178 | ||
4686ae4b PH |
179 | undefined = _Undefined() |
180 | ||
181 | ||
54256267 PH |
182 | class SWFInterpreter(object): |
183 | def __init__(self, file_contents): | |
fbf94a78 PH |
184 | self._patched_functions = { |
185 | (TimerClass, 'addEventListener'): lambda params: undefined, | |
186 | } | |
54256267 | 187 | code_tag = next(tag |
0cb20563 | 188 | for tag_code, tag in _extract_tags(file_contents) |
54256267 PH |
189 | if tag_code == 82) |
190 | p = code_tag.index(b'\0', 4) + 1 | |
191 | code_reader = io.BytesIO(code_tag[p:]) | |
192 | ||
193 | # Parse ABC (AVM2 ByteCode) | |
194 | ||
195 | # Define a couple convenience methods | |
196 | u30 = lambda *args: _u30(*args, reader=code_reader) | |
197 | s32 = lambda *args: _s32(*args, reader=code_reader) | |
198 | u32 = lambda *args: _u32(*args, reader=code_reader) | |
199 | read_bytes = lambda *args: _read_bytes(*args, reader=code_reader) | |
200 | read_byte = lambda *args: _read_byte(*args, reader=code_reader) | |
201 | ||
202 | # minor_version + major_version | |
203 | read_bytes(2 + 2) | |
204 | ||
205 | # Constant pool | |
206 | int_count = u30() | |
cd9ad1d7 | 207 | self.constant_ints = [0] |
54256267 | 208 | for _c in range(1, int_count): |
cd9ad1d7 PH |
209 | self.constant_ints.append(s32()) |
210 | self.constant_uints = [0] | |
54256267 PH |
211 | uint_count = u30() |
212 | for _c in range(1, uint_count): | |
cd9ad1d7 | 213 | self.constant_uints.append(u32()) |
54256267 | 214 | double_count = u30() |
0cb20563 | 215 | read_bytes(max(0, (double_count - 1)) * 8) |
54256267 | 216 | string_count = u30() |
70f767dc | 217 | self.constant_strings = [''] |
54256267 PH |
218 | for _c in range(1, string_count): |
219 | s = _read_string(code_reader) | |
70f767dc | 220 | self.constant_strings.append(s) |
54256267 PH |
221 | namespace_count = u30() |
222 | for _c in range(1, namespace_count): | |
223 | read_bytes(1) # kind | |
224 | u30() # name | |
225 | ns_set_count = u30() | |
226 | for _c in range(1, ns_set_count): | |
227 | count = u30() | |
228 | for _c2 in range(count): | |
229 | u30() | |
230 | multiname_count = u30() | |
231 | MULTINAME_SIZES = { | |
232 | 0x07: 2, # QName | |
233 | 0x0d: 2, # QNameA | |
234 | 0x0f: 1, # RTQName | |
235 | 0x10: 1, # RTQNameA | |
236 | 0x11: 0, # RTQNameL | |
237 | 0x12: 0, # RTQNameLA | |
238 | 0x09: 2, # Multiname | |
239 | 0x0e: 2, # MultinameA | |
240 | 0x1b: 1, # MultinameL | |
241 | 0x1c: 1, # MultinameLA | |
242 | } | |
243 | self.multinames = [''] | |
244 | for _c in range(1, multiname_count): | |
245 | kind = u30() | |
246 | assert kind in MULTINAME_SIZES, 'Invalid multiname kind %r' % kind | |
247 | if kind == 0x07: | |
248 | u30() # namespace_idx | |
249 | name_idx = u30() | |
70f767dc | 250 | self.multinames.append(self.constant_strings[name_idx]) |
4baafa22 PH |
251 | elif kind == 0x09: |
252 | name_idx = u30() | |
253 | u30() | |
254 | self.multinames.append(self.constant_strings[name_idx]) | |
54256267 | 255 | else: |
decf2ae4 | 256 | self.multinames.append(_Multiname(kind)) |
54256267 PH |
257 | for _c2 in range(MULTINAME_SIZES[kind]): |
258 | u30() | |
259 | ||
260 | # Methods | |
261 | method_count = u30() | |
262 | MethodInfo = collections.namedtuple( | |
263 | 'MethodInfo', | |
264 | ['NEED_ARGUMENTS', 'NEED_REST']) | |
265 | method_infos = [] | |
266 | for method_id in range(method_count): | |
267 | param_count = u30() | |
268 | u30() # return type | |
269 | for _ in range(param_count): | |
270 | u30() # param type | |
271 | u30() # name index (always 0 for youtube) | |
272 | flags = read_byte() | |
273 | if flags & 0x08 != 0: | |
274 | # Options present | |
275 | option_count = u30() | |
276 | for c in range(option_count): | |
277 | u30() # val | |
278 | read_bytes(1) # kind | |
279 | if flags & 0x80 != 0: | |
280 | # Param names present | |
281 | for _ in range(param_count): | |
282 | u30() # param name | |
283 | mi = MethodInfo(flags & 0x01 != 0, flags & 0x04 != 0) | |
284 | method_infos.append(mi) | |
285 | ||
286 | # Metadata | |
287 | metadata_count = u30() | |
288 | for _c in range(metadata_count): | |
289 | u30() # name | |
290 | item_count = u30() | |
291 | for _c2 in range(item_count): | |
292 | u30() # key | |
293 | u30() # value | |
294 | ||
295 | def parse_traits_info(): | |
296 | trait_name_idx = u30() | |
297 | kind_full = read_byte() | |
298 | kind = kind_full & 0x0f | |
299 | attrs = kind_full >> 4 | |
300 | methods = {} | |
cd9ad1d7 PH |
301 | constants = None |
302 | if kind == 0x00: # Slot | |
54256267 PH |
303 | u30() # Slot id |
304 | u30() # type_name_idx | |
305 | vindex = u30() | |
306 | if vindex != 0: | |
307 | read_byte() # vkind | |
cd9ad1d7 PH |
308 | elif kind == 0x06: # Const |
309 | u30() # Slot id | |
310 | u30() # type_name_idx | |
311 | vindex = u30() | |
312 | vkind = 'any' | |
313 | if vindex != 0: | |
314 | vkind = read_byte() | |
315 | if vkind == 0x03: # Constant_Int | |
316 | value = self.constant_ints[vindex] | |
317 | elif vkind == 0x04: # Constant_UInt | |
318 | value = self.constant_uints[vindex] | |
319 | else: | |
320 | return {}, None # Ignore silently for now | |
321 | constants = {self.multinames[trait_name_idx]: value} | |
322 | elif kind in (0x01, 0x02, 0x03): # Method / Getter / Setter | |
54256267 PH |
323 | u30() # disp_id |
324 | method_idx = u30() | |
325 | methods[self.multinames[trait_name_idx]] = method_idx | |
326 | elif kind == 0x04: # Class | |
327 | u30() # slot_id | |
328 | u30() # classi | |
329 | elif kind == 0x05: # Function | |
330 | u30() # slot_id | |
331 | function_idx = u30() | |
332 | methods[function_idx] = self.multinames[trait_name_idx] | |
333 | else: | |
334 | raise ExtractorError('Unsupported trait kind %d' % kind) | |
335 | ||
336 | if attrs & 0x4 != 0: # Metadata present | |
337 | metadata_count = u30() | |
338 | for _c3 in range(metadata_count): | |
339 | u30() # metadata index | |
340 | ||
cd9ad1d7 | 341 | return methods, constants |
54256267 PH |
342 | |
343 | # Classes | |
344 | class_count = u30() | |
345 | classes = [] | |
346 | for class_id in range(class_count): | |
347 | name_idx = u30() | |
01b4b745 PH |
348 | |
349 | cname = self.multinames[name_idx] | |
350 | avm_class = _AVMClass(name_idx, cname) | |
351 | classes.append(avm_class) | |
352 | ||
54256267 PH |
353 | u30() # super_name idx |
354 | flags = read_byte() | |
355 | if flags & 0x08 != 0: # Protected namespace is present | |
356 | u30() # protected_ns_idx | |
357 | intrf_count = u30() | |
358 | for _c2 in range(intrf_count): | |
359 | u30() | |
360 | u30() # iinit | |
361 | trait_count = u30() | |
362 | for _c2 in range(trait_count): | |
fbf94a78 | 363 | trait_methods, trait_constants = parse_traits_info() |
01b4b745 | 364 | avm_class.register_methods(trait_methods) |
fbf94a78 PH |
365 | if trait_constants: |
366 | avm_class.constants.update(trait_constants) | |
01b4b745 | 367 | |
54256267 PH |
368 | assert len(classes) == class_count |
369 | self._classes_by_name = dict((c.name, c) for c in classes) | |
370 | ||
371 | for avm_class in classes: | |
1921b245 | 372 | avm_class.cinit_idx = u30() |
54256267 PH |
373 | trait_count = u30() |
374 | for _c2 in range(trait_count): | |
cd9ad1d7 | 375 | trait_methods, trait_constants = parse_traits_info() |
01b4b745 | 376 | avm_class.register_methods(trait_methods) |
cd9ad1d7 PH |
377 | if trait_constants: |
378 | avm_class.constants.update(trait_constants) | |
54256267 PH |
379 | |
380 | # Scripts | |
381 | script_count = u30() | |
382 | for _c in range(script_count): | |
383 | u30() # init | |
384 | trait_count = u30() | |
385 | for _c2 in range(trait_count): | |
386 | parse_traits_info() | |
387 | ||
388 | # Method bodies | |
389 | method_body_count = u30() | |
390 | Method = collections.namedtuple('Method', ['code', 'local_count']) | |
1921b245 | 391 | self._all_methods = [] |
54256267 PH |
392 | for _c in range(method_body_count): |
393 | method_idx = u30() | |
394 | u30() # max_stack | |
395 | local_count = u30() | |
396 | u30() # init_scope_depth | |
397 | u30() # max_scope_depth | |
398 | code_length = u30() | |
399 | code = read_bytes(code_length) | |
1921b245 PH |
400 | m = Method(code, local_count) |
401 | self._all_methods.append(m) | |
54256267 PH |
402 | for avm_class in classes: |
403 | if method_idx in avm_class.method_idxs: | |
54256267 PH |
404 | avm_class.methods[avm_class.method_idxs[method_idx]] = m |
405 | exception_count = u30() | |
406 | for _c2 in range(exception_count): | |
407 | u30() # from | |
408 | u30() # to | |
409 | u30() # target | |
410 | u30() # exc_type | |
411 | u30() # var_name | |
412 | trait_count = u30() | |
413 | for _c2 in range(trait_count): | |
414 | parse_traits_info() | |
415 | ||
416 | assert p + code_reader.tell() == len(code_tag) | |
417 | ||
b7558d98 PH |
418 | def patch_function(self, avm_class, func_name, f): |
419 | self._patched_functions[(avm_class, func_name)] = f | |
420 | ||
1921b245 | 421 | def extract_class(self, class_name, call_cinit=True): |
54256267 | 422 | try: |
1921b245 | 423 | res = self._classes_by_name[class_name] |
54256267 PH |
424 | except KeyError: |
425 | raise ExtractorError('Class %r not found' % class_name) | |
426 | ||
1921b245 PH |
427 | if call_cinit and hasattr(res, 'cinit_idx'): |
428 | res.register_methods({'$cinit': res.cinit_idx}) | |
429 | res.methods['$cinit'] = self._all_methods[res.cinit_idx] | |
430 | cinit = self.extract_function(res, '$cinit') | |
431 | cinit([]) | |
432 | ||
433 | return res | |
434 | ||
54256267 | 435 | def extract_function(self, avm_class, func_name): |
b7558d98 PH |
436 | p = self._patched_functions.get((avm_class, func_name)) |
437 | if p: | |
438 | return p | |
54256267 PH |
439 | if func_name in avm_class.method_pyfunctions: |
440 | return avm_class.method_pyfunctions[func_name] | |
441 | if func_name in self._classes_by_name: | |
442 | return self._classes_by_name[func_name].make_object() | |
443 | if func_name not in avm_class.methods: | |
01b4b745 PH |
444 | raise ExtractorError('Cannot find function %s.%s' % ( |
445 | avm_class.name, func_name)) | |
54256267 PH |
446 | m = avm_class.methods[func_name] |
447 | ||
448 | def resfunc(args): | |
449 | # Helper functions | |
450 | coder = io.BytesIO(m.code) | |
451 | s24 = lambda: _s24(coder) | |
452 | u30 = lambda: _u30(coder) | |
453 | ||
e75c24e8 | 454 | registers = [avm_class.variables] + list(args) + [None] * m.local_count |
54256267 | 455 | stack = [] |
01b4b745 | 456 | scopes = collections.deque([ |
fbf94a78 | 457 | self._classes_by_name, avm_class.constants, avm_class.variables]) |
54256267 PH |
458 | while True: |
459 | opcode = _read_byte(coder) | |
33a266f4 PH |
460 | if opcode == 9: # label |
461 | pass # Spec says: "Do nothing." | |
462 | elif opcode == 16: # jump | |
e983cf52 PH |
463 | offset = s24() |
464 | coder.seek(coder.tell() + offset) | |
465 | elif opcode == 17: # iftrue | |
54256267 PH |
466 | offset = s24() |
467 | value = stack.pop() | |
468 | if value: | |
469 | coder.seek(coder.tell() + offset) | |
e75c24e8 PH |
470 | elif opcode == 18: # iffalse |
471 | offset = s24() | |
472 | value = stack.pop() | |
473 | if not value: | |
474 | coder.seek(coder.tell() + offset) | |
e983cf52 PH |
475 | elif opcode == 19: # ifeq |
476 | offset = s24() | |
477 | value2 = stack.pop() | |
478 | value1 = stack.pop() | |
479 | if value2 == value1: | |
480 | coder.seek(coder.tell() + offset) | |
481 | elif opcode == 20: # ifne | |
482 | offset = s24() | |
483 | value2 = stack.pop() | |
484 | value1 = stack.pop() | |
485 | if value2 != value1: | |
486 | coder.seek(coder.tell() + offset) | |
33a266f4 PH |
487 | elif opcode == 21: # iflt |
488 | offset = s24() | |
489 | value2 = stack.pop() | |
490 | value1 = stack.pop() | |
491 | if value1 < value2: | |
492 | coder.seek(coder.tell() + offset) | |
e983cf52 PH |
493 | elif opcode == 32: # pushnull |
494 | stack.append(None) | |
4686ae4b PH |
495 | elif opcode == 33: # pushundefined |
496 | stack.append(undefined) | |
54256267 PH |
497 | elif opcode == 36: # pushbyte |
498 | v = _read_byte(coder) | |
499 | stack.append(v) | |
162f54ec PH |
500 | elif opcode == 37: # pushshort |
501 | v = u30() | |
502 | stack.append(v) | |
a4bb8395 PH |
503 | elif opcode == 38: # pushtrue |
504 | stack.append(True) | |
505 | elif opcode == 39: # pushfalse | |
506 | stack.append(False) | |
4686ae4b PH |
507 | elif opcode == 40: # pushnan |
508 | stack.append(float('NaN')) | |
0cb20563 PH |
509 | elif opcode == 42: # dup |
510 | value = stack[-1] | |
511 | stack.append(value) | |
54256267 PH |
512 | elif opcode == 44: # pushstring |
513 | idx = u30() | |
70f767dc | 514 | stack.append(self.constant_strings[idx]) |
54256267 | 515 | elif opcode == 48: # pushscope |
54256267 | 516 | new_scope = stack.pop() |
e75c24e8 | 517 | scopes.append(new_scope) |
decf2ae4 PH |
518 | elif opcode == 66: # construct |
519 | arg_count = u30() | |
520 | args = list(reversed( | |
521 | [stack.pop() for _ in range(arg_count)])) | |
522 | obj = stack.pop() | |
523 | res = obj.avm_class.make_object() | |
524 | stack.append(res) | |
54256267 PH |
525 | elif opcode == 70: # callproperty |
526 | index = u30() | |
527 | mname = self.multinames[index] | |
528 | arg_count = u30() | |
529 | args = list(reversed( | |
530 | [stack.pop() for _ in range(arg_count)])) | |
531 | obj = stack.pop() | |
01b4b745 | 532 | |
6b592d93 PH |
533 | if obj == StringClass: |
534 | if mname == 'String': | |
535 | assert len(args) == 1 | |
536 | assert isinstance(args[0], ( | |
537 | int, compat_str, _Undefined)) | |
538 | if args[0] == undefined: | |
539 | res = 'undefined' | |
540 | else: | |
541 | res = compat_str(args[0]) | |
542 | stack.append(res) | |
543 | continue | |
544 | else: | |
545 | raise NotImplementedError( | |
546 | 'Function String.%s is not yet implemented' | |
547 | % mname) | |
548 | elif isinstance(obj, _AVMClass_Object): | |
01b4b745 PH |
549 | func = self.extract_function(obj.avm_class, mname) |
550 | res = func(args) | |
54256267 | 551 | stack.append(res) |
01b4b745 | 552 | continue |
6b592d93 PH |
553 | elif isinstance(obj, _AVMClass): |
554 | func = self.extract_function(obj, mname) | |
555 | res = func(args) | |
556 | stack.append(res) | |
557 | continue | |
0d989011 PH |
558 | elif isinstance(obj, _ScopeDict): |
559 | if mname in obj.avm_class.method_names: | |
560 | func = self.extract_function(obj.avm_class, mname) | |
561 | res = func(args) | |
562 | else: | |
563 | res = obj[mname] | |
564 | stack.append(res) | |
565 | continue | |
01b4b745 PH |
566 | elif isinstance(obj, compat_str): |
567 | if mname == 'split': | |
568 | assert len(args) == 1 | |
569 | assert isinstance(args[0], compat_str) | |
570 | if args[0] == '': | |
571 | res = list(obj) | |
572 | else: | |
573 | res = obj.split(args[0]) | |
574 | stack.append(res) | |
575 | continue | |
33a266f4 PH |
576 | elif mname == 'charCodeAt': |
577 | assert len(args) <= 1 | |
578 | idx = 0 if len(args) == 0 else args[0] | |
579 | assert isinstance(idx, int) | |
580 | res = ord(obj[idx]) | |
581 | stack.append(res) | |
582 | continue | |
01b4b745 PH |
583 | elif isinstance(obj, list): |
584 | if mname == 'slice': | |
585 | assert len(args) == 1 | |
586 | assert isinstance(args[0], int) | |
587 | res = obj[args[0]:] | |
588 | stack.append(res) | |
589 | continue | |
590 | elif mname == 'join': | |
591 | assert len(args) == 1 | |
592 | assert isinstance(args[0], compat_str) | |
593 | res = args[0].join(obj) | |
594 | stack.append(res) | |
595 | continue | |
596 | raise NotImplementedError( | |
597 | 'Unsupported property %r on %r' | |
598 | % (mname, obj)) | |
8d05f2c1 | 599 | elif opcode == 71: # returnvoid |
4686ae4b | 600 | res = undefined |
8d05f2c1 | 601 | return res |
54256267 PH |
602 | elif opcode == 72: # returnvalue |
603 | res = stack.pop() | |
604 | return res | |
fbf94a78 PH |
605 | elif opcode == 73: # constructsuper |
606 | # Not yet implemented, just hope it works without it | |
607 | arg_count = u30() | |
608 | args = list(reversed( | |
609 | [stack.pop() for _ in range(arg_count)])) | |
610 | obj = stack.pop() | |
54256267 PH |
611 | elif opcode == 74: # constructproperty |
612 | index = u30() | |
613 | arg_count = u30() | |
614 | args = list(reversed( | |
615 | [stack.pop() for _ in range(arg_count)])) | |
616 | obj = stack.pop() | |
617 | ||
618 | mname = self.multinames[index] | |
01b4b745 | 619 | assert isinstance(obj, _AVMClass) |
7fbf54dc | 620 | |
54256267 PH |
621 | # We do not actually call the constructor for now; |
622 | # we just pretend it does nothing | |
01b4b745 | 623 | stack.append(obj.make_object()) |
54256267 PH |
624 | elif opcode == 79: # callpropvoid |
625 | index = u30() | |
626 | mname = self.multinames[index] | |
627 | arg_count = u30() | |
628 | args = list(reversed( | |
629 | [stack.pop() for _ in range(arg_count)])) | |
630 | obj = stack.pop() | |
8d05f2c1 PH |
631 | if isinstance(obj, _AVMClass_Object): |
632 | func = self.extract_function(obj.avm_class, mname) | |
633 | res = func(args) | |
4686ae4b | 634 | assert res is undefined |
8d05f2c1 PH |
635 | continue |
636 | if isinstance(obj, _ScopeDict): | |
637 | assert mname in obj.avm_class.method_names | |
638 | func = self.extract_function(obj.avm_class, mname) | |
639 | res = func(args) | |
4686ae4b | 640 | assert res is undefined |
8d05f2c1 | 641 | continue |
54256267 PH |
642 | if mname == 'reverse': |
643 | assert isinstance(obj, list) | |
644 | obj.reverse() | |
645 | else: | |
646 | raise NotImplementedError( | |
647 | 'Unsupported (void) property %r on %r' | |
648 | % (mname, obj)) | |
649 | elif opcode == 86: # newarray | |
650 | arg_count = u30() | |
651 | arr = [] | |
652 | for i in range(arg_count): | |
653 | arr.append(stack.pop()) | |
654 | arr = arr[::-1] | |
655 | stack.append(arr) | |
70f767dc PH |
656 | elif opcode == 93: # findpropstrict |
657 | index = u30() | |
658 | mname = self.multinames[index] | |
659 | for s in reversed(scopes): | |
660 | if mname in s: | |
661 | res = s | |
662 | break | |
663 | else: | |
664 | res = scopes[0] | |
6b592d93 PH |
665 | if mname not in res and mname in _builtin_classes: |
666 | stack.append(_builtin_classes[mname]) | |
3cbcff8a PH |
667 | else: |
668 | stack.append(res[mname]) | |
54256267 PH |
669 | elif opcode == 94: # findproperty |
670 | index = u30() | |
671 | mname = self.multinames[index] | |
e75c24e8 PH |
672 | for s in reversed(scopes): |
673 | if mname in s: | |
674 | res = s | |
675 | break | |
676 | else: | |
01b4b745 | 677 | res = avm_class.variables |
54256267 PH |
678 | stack.append(res) |
679 | elif opcode == 96: # getlex | |
680 | index = u30() | |
681 | mname = self.multinames[index] | |
e75c24e8 PH |
682 | for s in reversed(scopes): |
683 | if mname in s: | |
684 | scope = s | |
685 | break | |
686 | else: | |
01b4b745 | 687 | scope = avm_class.variables |
cd9ad1d7 PH |
688 | |
689 | if mname in scope: | |
690 | res = scope[mname] | |
fbf94a78 PH |
691 | elif mname in _builtin_classes: |
692 | res = _builtin_classes[mname] | |
cd9ad1d7 | 693 | else: |
dfb1b146 | 694 | # Assume uninitialized |
f48d3e9b | 695 | # TODO warn here |
fbf94a78 | 696 | res = undefined |
54256267 PH |
697 | stack.append(res) |
698 | elif opcode == 97: # setproperty | |
699 | index = u30() | |
700 | value = stack.pop() | |
701 | idx = self.multinames[index] | |
decf2ae4 PH |
702 | if isinstance(idx, _Multiname): |
703 | idx = stack.pop() | |
54256267 PH |
704 | obj = stack.pop() |
705 | obj[idx] = value | |
706 | elif opcode == 98: # getlocal | |
707 | index = u30() | |
708 | stack.append(registers[index]) | |
709 | elif opcode == 99: # setlocal | |
710 | index = u30() | |
711 | value = stack.pop() | |
712 | registers[index] = value | |
713 | elif opcode == 102: # getproperty | |
714 | index = u30() | |
715 | pname = self.multinames[index] | |
716 | if pname == 'length': | |
717 | obj = stack.pop() | |
3cbcff8a | 718 | assert isinstance(obj, (compat_str, list)) |
54256267 | 719 | stack.append(len(obj)) |
4baafa22 PH |
720 | elif isinstance(pname, compat_str): # Member access |
721 | obj = stack.pop() | |
fbf94a78 PH |
722 | if isinstance(obj, _AVMClass): |
723 | res = obj.static_properties[pname] | |
724 | stack.append(res) | |
725 | continue | |
726 | ||
727 | assert isinstance(obj, (dict, _ScopeDict)),\ | |
0ab1ca55 | 728 | 'Accessing member %r on %r' % (pname, obj) |
4686ae4b | 729 | res = obj.get(pname, undefined) |
8d05f2c1 | 730 | stack.append(res) |
54256267 PH |
731 | else: # Assume attribute access |
732 | idx = stack.pop() | |
733 | assert isinstance(idx, int) | |
734 | obj = stack.pop() | |
735 | assert isinstance(obj, list) | |
736 | stack.append(obj[idx]) | |
1921b245 PH |
737 | elif opcode == 104: # initproperty |
738 | index = u30() | |
739 | value = stack.pop() | |
740 | idx = self.multinames[index] | |
741 | if isinstance(idx, _Multiname): | |
742 | idx = stack.pop() | |
743 | obj = stack.pop() | |
744 | obj[idx] = value | |
0cb20563 PH |
745 | elif opcode == 115: # convert_ |
746 | value = stack.pop() | |
747 | intvalue = int(value) | |
748 | stack.append(intvalue) | |
54256267 PH |
749 | elif opcode == 128: # coerce |
750 | u30() | |
4686ae4b PH |
751 | elif opcode == 130: # coerce_a |
752 | value = stack.pop() | |
753 | # um, yes, it's any value | |
754 | stack.append(value) | |
54256267 PH |
755 | elif opcode == 133: # coerce_s |
756 | assert isinstance(stack[-1], (type(None), compat_str)) | |
4686ae4b PH |
757 | elif opcode == 147: # decrement |
758 | value = stack.pop() | |
759 | assert isinstance(value, int) | |
760 | stack.append(value - 1) | |
761 | elif opcode == 149: # typeof | |
762 | value = stack.pop() | |
763 | return { | |
764 | _Undefined: 'undefined', | |
765 | compat_str: 'String', | |
766 | int: 'Number', | |
767 | float: 'Number', | |
768 | }[type(value)] | |
0cb20563 PH |
769 | elif opcode == 160: # add |
770 | value2 = stack.pop() | |
771 | value1 = stack.pop() | |
772 | res = value1 + value2 | |
773 | stack.append(res) | |
774 | elif opcode == 161: # subtract | |
775 | value2 = stack.pop() | |
776 | value1 = stack.pop() | |
777 | res = value1 - value2 | |
778 | stack.append(res) | |
33a266f4 PH |
779 | elif opcode == 162: # multiply |
780 | value2 = stack.pop() | |
781 | value1 = stack.pop() | |
782 | res = value1 * value2 | |
783 | stack.append(res) | |
54256267 PH |
784 | elif opcode == 164: # modulo |
785 | value2 = stack.pop() | |
786 | value1 = stack.pop() | |
787 | res = value1 % value2 | |
788 | stack.append(res) | |
162f54ec PH |
789 | elif opcode == 168: # bitand |
790 | value2 = stack.pop() | |
791 | value1 = stack.pop() | |
792 | assert isinstance(value1, int) | |
793 | assert isinstance(value2, int) | |
794 | res = value1 & value2 | |
795 | stack.append(res) | |
eb537604 PH |
796 | elif opcode == 171: # equals |
797 | value2 = stack.pop() | |
798 | value1 = stack.pop() | |
799 | result = value1 == value2 | |
800 | stack.append(result) | |
54256267 PH |
801 | elif opcode == 175: # greaterequals |
802 | value2 = stack.pop() | |
803 | value1 = stack.pop() | |
804 | result = value1 >= value2 | |
805 | stack.append(result) | |
33a266f4 PH |
806 | elif opcode == 192: # increment_i |
807 | value = stack.pop() | |
808 | assert isinstance(value, int) | |
809 | stack.append(value + 1) | |
54256267 PH |
810 | elif opcode == 208: # getlocal_0 |
811 | stack.append(registers[0]) | |
812 | elif opcode == 209: # getlocal_1 | |
813 | stack.append(registers[1]) | |
814 | elif opcode == 210: # getlocal_2 | |
815 | stack.append(registers[2]) | |
816 | elif opcode == 211: # getlocal_3 | |
817 | stack.append(registers[3]) | |
70f767dc PH |
818 | elif opcode == 212: # setlocal_0 |
819 | registers[0] = stack.pop() | |
820 | elif opcode == 213: # setlocal_1 | |
821 | registers[1] = stack.pop() | |
54256267 PH |
822 | elif opcode == 214: # setlocal_2 |
823 | registers[2] = stack.pop() | |
824 | elif opcode == 215: # setlocal_3 | |
825 | registers[3] = stack.pop() | |
826 | else: | |
827 | raise NotImplementedError( | |
828 | 'Unsupported opcode %d' % opcode) | |
829 | ||
830 | avm_class.method_pyfunctions[func_name] = resfunc | |
831 | return resfunc |