swfinterp.py 26 KB

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