[swfinterp] Implement String basics
authorPhilipp Hagemeister <phihag@phihag.de>
Mon, 17 Nov 2014 03:25:04 +0000 (04:25 +0100)
committerPhilipp Hagemeister <phihag@phihag.de>
Mon, 17 Nov 2014 03:25:10 +0000 (04:25 +0100)
test/swftests/StringBasics.as [new file with mode: 0644]
test/swftests/StringConversion.as [new file with mode: 0644]
youtube_dl/swfinterp.py

diff --git a/test/swftests/StringBasics.as b/test/swftests/StringBasics.as
new file mode 100644 (file)
index 0000000..d27430b
--- /dev/null
@@ -0,0 +1,11 @@
+// input: []
+// output: 3
+
+package {
+public class StringBasics {
+    public static function main():int{
+        var s:String = "abc";
+        return s.length;
+    }
+}
+}
diff --git a/test/swftests/StringConversion.as b/test/swftests/StringConversion.as
new file mode 100644 (file)
index 0000000..c976f50
--- /dev/null
@@ -0,0 +1,11 @@
+// input: []
+// output: 2
+
+package {
+public class StringConversion {
+    public static function main():int{
+        var s:String = String(99);
+        return s.length;
+    }
+}
+}
index 85efde5924cddb12988260510da68a296f4241b0..0ce8474ca7ce96d77d05b9c9c5243a6632b69591 100644 (file)
@@ -148,6 +148,9 @@ def _read_byte(reader):
     return res
 
 
+StringClass = _AVMClass('(no name idx)', 'String')
+
+
 class SWFInterpreter(object):
     def __init__(self, file_contents):
         self._patched_functions = {}
@@ -483,6 +486,17 @@ class SWFInterpreter(object):
                             res = args[0].join(obj)
                             stack.append(res)
                             continue
+                    elif obj == StringClass:
+                        if mname == 'String':
+                            assert len(args) == 1
+                            assert isinstance(args[0], (int, compat_str))
+                            res = compat_str(args[0])
+                            stack.append(res)
+                            continue
+                        else:
+                            raise NotImplementedError(
+                                'Function String.%s is not yet implemented'
+                                % mname)
                     raise NotImplementedError(
                         'Unsupported property %r on %r'
                         % (mname, obj))
@@ -532,7 +546,10 @@ class SWFInterpreter(object):
                             break
                     else:
                         res = scopes[0]
-                    stack.append(res[mname])
+                    if mname not in res and mname == 'String':
+                        stack.append(StringClass)
+                    else:
+                        stack.append(res[mname])
                 elif opcode == 94:  # findproperty
                     index = u30()
                     mname = self.multinames[index]
@@ -576,7 +593,7 @@ class SWFInterpreter(object):
                     pname = self.multinames[index]
                     if pname == 'length':
                         obj = stack.pop()
-                        assert isinstance(obj, list)
+                        assert isinstance(obj, (compat_str, list))
                         stack.append(len(obj))
                     elif isinstance(pname, compat_str):  # Member access
                         obj = stack.pop()