]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/swfinterp.py
7cf490aa43a878b3c377bea0b173c7a2b170c2c7
[youtubedl] / youtube_dl / swfinterp.py
1 from __future__ import unicode_literals
2
3 import collections
4 import io
5 import zlib
6
7 from .compat import (
8 compat_str,
9 compat_struct_unpack,
10 )
11 from .utils import (
12 ExtractorError,
13 )
14
15
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
28 framesize_nbits = compat_struct_unpack('!B', content[:1])[0] >> 3
29 framesize_len = (5 + 4 * framesize_nbits + 7) // 8
30
31 pos = framesize_len + 2 + 2
32 while pos < len(content):
33 header16 = compat_struct_unpack('<H', content[pos:pos + 2])[0]
34 pos += 2
35 tag_code = header16 >> 6
36 tag_len = header16 & 0x3f
37 if tag_len == 0x3f:
38 tag_len = compat_struct_unpack('<I', content[pos:pos + 4])[0]
39 pos += 4
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)))
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
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
66 class _AVMClass(object):
67 def __init__(self, name_idx, name, static_properties=None):
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 = {}
74 self.static_properties = static_properties if static_properties else {}
75
76 self.variables = _ScopeDict(self)
77 self.constants = {}
78
79 def make_object(self):
80 return _AVMClass_Object(self)
81
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
91
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
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
106 b = compat_struct_unpack('<B', buf)[0]
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
118 _u32 = _read_int
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
131 last_byte = b'\xff' if (ord(bs[2:3]) >= 0x80) else b'\x00'
132 return compat_struct_unpack('<i', bs + last_byte)[0]
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):
143 assert count >= 0
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)
151 res = compat_struct_unpack('<B', resb)[0]
152 return res
153
154
155 StringClass = _AVMClass('(no name idx)', 'String')
156 ByteArrayClass = _AVMClass('(no name idx)', 'ByteArray')
157 TimerClass = _AVMClass('(no name idx)', 'Timer')
158 TimerEventClass = _AVMClass('(no name idx)', 'TimerEvent', {'TIMER': 'timer'})
159 _builtin_classes = {
160 StringClass.name: StringClass,
161 ByteArrayClass.name: ByteArrayClass,
162 TimerClass.name: TimerClass,
163 TimerEventClass.name: TimerEventClass,
164 }
165
166
167 class _Undefined(object):
168 def __bool__(self):
169 return False
170 __nonzero__ = __bool__
171
172 def __hash__(self):
173 return 0
174
175 def __str__(self):
176 return 'undefined'
177 __repr__ = __str__
178
179 undefined = _Undefined()
180
181
182 class SWFInterpreter(object):
183 def __init__(self, file_contents):
184 self._patched_functions = {
185 (TimerClass, 'addEventListener'): lambda params: undefined,
186 }
187 code_tag = next(tag
188 for tag_code, tag in _extract_tags(file_contents)
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()
207 self.constant_ints = [0]
208 for _c in range(1, int_count):
209 self.constant_ints.append(s32())
210 self.constant_uints = [0]
211 uint_count = u30()
212 for _c in range(1, uint_count):
213 self.constant_uints.append(u32())
214 double_count = u30()
215 read_bytes(max(0, (double_count - 1)) * 8)
216 string_count = u30()
217 self.constant_strings = ['']
218 for _c in range(1, string_count):
219 s = _read_string(code_reader)
220 self.constant_strings.append(s)
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()
250 self.multinames.append(self.constant_strings[name_idx])
251 elif kind == 0x09:
252 name_idx = u30()
253 u30()
254 self.multinames.append(self.constant_strings[name_idx])
255 else:
256 self.multinames.append(_Multiname(kind))
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 = {}
301 constants = None
302 if kind == 0x00: # Slot
303 u30() # Slot id
304 u30() # type_name_idx
305 vindex = u30()
306 if vindex != 0:
307 read_byte() # vkind
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
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
341 return methods, constants
342
343 # Classes
344 class_count = u30()
345 classes = []
346 for class_id in range(class_count):
347 name_idx = u30()
348
349 cname = self.multinames[name_idx]
350 avm_class = _AVMClass(name_idx, cname)
351 classes.append(avm_class)
352
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):
363 trait_methods, trait_constants = parse_traits_info()
364 avm_class.register_methods(trait_methods)
365 if trait_constants:
366 avm_class.constants.update(trait_constants)
367
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:
372 avm_class.cinit_idx = u30()
373 trait_count = u30()
374 for _c2 in range(trait_count):
375 trait_methods, trait_constants = parse_traits_info()
376 avm_class.register_methods(trait_methods)
377 if trait_constants:
378 avm_class.constants.update(trait_constants)
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'])
391 self._all_methods = []
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)
400 m = Method(code, local_count)
401 self._all_methods.append(m)
402 for avm_class in classes:
403 if method_idx in avm_class.method_idxs:
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
418 def patch_function(self, avm_class, func_name, f):
419 self._patched_functions[(avm_class, func_name)] = f
420
421 def extract_class(self, class_name, call_cinit=True):
422 try:
423 res = self._classes_by_name[class_name]
424 except KeyError:
425 raise ExtractorError('Class %r not found' % class_name)
426
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
435 def extract_function(self, avm_class, func_name):
436 p = self._patched_functions.get((avm_class, func_name))
437 if p:
438 return p
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:
444 raise ExtractorError('Cannot find function %s.%s' % (
445 avm_class.name, func_name))
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
454 registers = [avm_class.variables] + list(args) + [None] * m.local_count
455 stack = []
456 scopes = collections.deque([
457 self._classes_by_name, avm_class.constants, avm_class.variables])
458 while True:
459 opcode = _read_byte(coder)
460 if opcode == 9: # label
461 pass # Spec says: "Do nothing."
462 elif opcode == 16: # jump
463 offset = s24()
464 coder.seek(coder.tell() + offset)
465 elif opcode == 17: # iftrue
466 offset = s24()
467 value = stack.pop()
468 if value:
469 coder.seek(coder.tell() + offset)
470 elif opcode == 18: # iffalse
471 offset = s24()
472 value = stack.pop()
473 if not value:
474 coder.seek(coder.tell() + offset)
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)
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)
493 elif opcode == 32: # pushnull
494 stack.append(None)
495 elif opcode == 33: # pushundefined
496 stack.append(undefined)
497 elif opcode == 36: # pushbyte
498 v = _read_byte(coder)
499 stack.append(v)
500 elif opcode == 37: # pushshort
501 v = u30()
502 stack.append(v)
503 elif opcode == 38: # pushtrue
504 stack.append(True)
505 elif opcode == 39: # pushfalse
506 stack.append(False)
507 elif opcode == 40: # pushnan
508 stack.append(float('NaN'))
509 elif opcode == 42: # dup
510 value = stack[-1]
511 stack.append(value)
512 elif opcode == 44: # pushstring
513 idx = u30()
514 stack.append(self.constant_strings[idx])
515 elif opcode == 48: # pushscope
516 new_scope = stack.pop()
517 scopes.append(new_scope)
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)
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()
532
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):
549 func = self.extract_function(obj.avm_class, mname)
550 res = func(args)
551 stack.append(res)
552 continue
553 elif isinstance(obj, _AVMClass):
554 func = self.extract_function(obj, mname)
555 res = func(args)
556 stack.append(res)
557 continue
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
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
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
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))
599 elif opcode == 71: # returnvoid
600 res = undefined
601 return res
602 elif opcode == 72: # returnvalue
603 res = stack.pop()
604 return res
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()
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]
619 assert isinstance(obj, _AVMClass)
620
621 # We do not actually call the constructor for now;
622 # we just pretend it does nothing
623 stack.append(obj.make_object())
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()
631 if isinstance(obj, _AVMClass_Object):
632 func = self.extract_function(obj.avm_class, mname)
633 res = func(args)
634 assert res is undefined
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)
640 assert res is undefined
641 continue
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)
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]
665 if mname not in res and mname in _builtin_classes:
666 stack.append(_builtin_classes[mname])
667 else:
668 stack.append(res[mname])
669 elif opcode == 94: # findproperty
670 index = u30()
671 mname = self.multinames[index]
672 for s in reversed(scopes):
673 if mname in s:
674 res = s
675 break
676 else:
677 res = avm_class.variables
678 stack.append(res)
679 elif opcode == 96: # getlex
680 index = u30()
681 mname = self.multinames[index]
682 for s in reversed(scopes):
683 if mname in s:
684 scope = s
685 break
686 else:
687 scope = avm_class.variables
688
689 if mname in scope:
690 res = scope[mname]
691 elif mname in _builtin_classes:
692 res = _builtin_classes[mname]
693 else:
694 # Assume uninitialized
695 # TODO warn here
696 res = undefined
697 stack.append(res)
698 elif opcode == 97: # setproperty
699 index = u30()
700 value = stack.pop()
701 idx = self.multinames[index]
702 if isinstance(idx, _Multiname):
703 idx = stack.pop()
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()
718 assert isinstance(obj, (compat_str, list))
719 stack.append(len(obj))
720 elif isinstance(pname, compat_str): # Member access
721 obj = stack.pop()
722 if isinstance(obj, _AVMClass):
723 res = obj.static_properties[pname]
724 stack.append(res)
725 continue
726
727 assert isinstance(obj, (dict, _ScopeDict)),\
728 'Accessing member %r on %r' % (pname, obj)
729 res = obj.get(pname, undefined)
730 stack.append(res)
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])
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
745 elif opcode == 115: # convert_
746 value = stack.pop()
747 intvalue = int(value)
748 stack.append(intvalue)
749 elif opcode == 128: # coerce
750 u30()
751 elif opcode == 130: # coerce_a
752 value = stack.pop()
753 # um, yes, it's any value
754 stack.append(value)
755 elif opcode == 133: # coerce_s
756 assert isinstance(stack[-1], (type(None), compat_str))
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)]
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)
779 elif opcode == 162: # multiply
780 value2 = stack.pop()
781 value1 = stack.pop()
782 res = value1 * value2
783 stack.append(res)
784 elif opcode == 164: # modulo
785 value2 = stack.pop()
786 value1 = stack.pop()
787 res = value1 % value2
788 stack.append(res)
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)
796 elif opcode == 171: # equals
797 value2 = stack.pop()
798 value1 = stack.pop()
799 result = value1 == value2
800 stack.append(result)
801 elif opcode == 175: # greaterequals
802 value2 = stack.pop()
803 value1 = stack.pop()
804 result = value1 >= value2
805 stack.append(result)
806 elif opcode == 192: # increment_i
807 value = stack.pop()
808 assert isinstance(value, int)
809 stack.append(value + 1)
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])
818 elif opcode == 212: # setlocal_0
819 registers[0] = stack.pop()
820 elif opcode == 213: # setlocal_1
821 registers[1] = stack.pop()
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