swfinterp.py 29 KB

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