Commit Graph

2270 Commits

Author SHA1 Message Date
David Wendt eaebd3c63c Make `Avm2MethodEntry` hold it's `TranslationUnit` rather than an `AbcFile`. 2020-07-13 17:45:03 -04:00
David Wendt 4467bc3193 Make `TranslationUnit` a GC-mandatory type (only referred to by `GcCell`). 2020-07-13 17:45:03 -04:00
David Wendt 60f3ae3ba7 Remove `Avm2ScriptEntry`. It is now obsolete and unused. 2020-07-13 17:45:02 -04:00
David Wendt f549d0146e Fix compilation bugs involved with automatic script initializer execution. 2020-07-13 17:45:02 -04:00
David Wendt b4f944b37b Wrap ABC loading inside of a `TranslationUnit`. 2020-07-13 17:45:01 -04:00
David Wendt 70e9030072 Decouple the entire trait machinery from ABC-provided traits.
This commit breaks the build: we still need to tell `Avm2` how to turn ABC traits into our own internal `Trait<'gc>`, `Class<'gc>`, and `Method<'gc>` types. We also need something to track which traits have already been instantiated, because `callstatic` would otherwise reinstantiate the trait in a different scope. (In fact, I think it *does* do exactly that right now...)
2020-07-13 17:45:01 -04:00
David Wendt 15a62d31cb Add an internal representation of `Trait`, separate from `swf::avm2::types::Trait`, which is specific to the ABC file format.
The intention is to completely replace all usage of `Avm2XYZEntry` with `Class`, `Trait`, and `Method`. This will allow runtime-provided global class traits to coexist with those provided by user code.
2020-07-13 17:45:01 -04:00
David Wendt 4cd30455de Excise `ReturnValue<'gc>` from all `TObject` methods.
Inspired by Dinnerbone's PR doing the exact same thing to AVM1.

On AVM2 we have a bit of a subtle issue: the base implementation of `set_property_local` and `init_property_local` *must* return `ReturnValue`s to avoid double-borrows. Each implementation of `TObject` must resolve them before returning.
2020-07-13 17:45:00 -04:00
David Wendt 3362ec09e8 chore: Clippy conformance 2020-07-13 17:45:00 -04:00
David Wendt fe283e6770 Silence this warning about occupied slots being an unused variant.
I don't know if I'm missing something, but I'm pretty sure this variant is reachable via `TObject::install_slot`.
2020-07-13 17:45:00 -04:00
David Wendt 6117288fe2 Add tests for `ifstricteq`, `ifstrictne`, and `strictequals`. 2020-07-13 17:44:59 -04:00
David Wendt 34ab8c8ce6 `NaN` is not special-cased in AS3. 2020-07-13 17:44:59 -04:00
David Wendt 34b3bbae63 *Correctly* implement `ifstricteq` and `ifstrictne`.
The previous implementation suffered from copypasta and was attempting to assert that both of it's values were `bool`.
2020-07-13 17:44:59 -04:00
David Wendt ecfd16cec9 Add global constants `undefined`, `null`, and `NaN`. 2020-07-13 17:44:58 -04:00
David Wendt 03a240ebcd Add tests for `valueOf`. 2020-07-13 17:44:58 -04:00
David Wendt b4d907bf2e Implement `strictequals`. 2020-07-13 17:44:58 -04:00
David Wendt 3b52dfe2ba Since we have an `es3_inheritance` test now, rename the existing inheritance test to `es4_inheritance`. 2020-07-13 17:44:57 -04:00
David Wendt cf6714d33c Implement and test `toLocaleString`.
This function has vague documentation about enabling locale-specific formatting in subclasses. As far as I can tell, none of the objects I implemented so far do anything different than `toString`, so I just have it use the same `TObject` property I set up for `toString`.
2020-07-13 17:44:57 -04:00
David Wendt 3558c3afa0 Add test of `Function.prototype.call` 2020-07-13 17:44:56 -04:00
David Wendt 16774aa055 Add a test for legacy / ES3 inheritance.
This was originally something *way* more evil: mixed inheritance between ES3 and ES4 classes. It didn't pan out due to fundamental limitations of the two object models. How the hell did Brendan Eich/Adobe/TC-39 expect ES4 classes to be adopted in already-existing codebases?!
2020-07-13 17:44:56 -04:00
David Wendt 4b66af8dc3 ES4 classes, while superficially similar to functions, are not functions and should not inherit from the `Function` prototype.
We still reuse the `FunctionObject` machinery internally. If necessary, we may want to split this into a separate `ClassObject` if some internal `TObject` method needs replacing for classes.
2020-07-13 17:44:55 -04:00
David Wendt 8b36751fbb Several built-in functions are not `public`, but instead live in the `AS3` namespace. This moves those functions there.
In practice not many movies will care about this, because the `AS3` namespace is open by default. You could opt-out of that, and I suppose that was there for using existing ES3 code in AS3 projects. ES4 would have had a similar ES4 namespace, which "JavaScript 2.0" code would need to opt into. Of course, ES4/JS2 never happened, so we just have this weird historical quirk here.
2020-07-13 17:44:55 -04:00
David Wendt 37cdcb3bce Add tests for `toString` on objects, functions, and classes. 2020-07-13 17:44:54 -04:00
David Wendt f493cf954f Make `toString` and `valueOf` methods of `TObject`, called `to_string` and `value_of` respectively.
The reason for this is that, in AVM2, `toString` and `valueOf` are not defined on the classes or prototypes of `Function` or `Class`. Instead, they use the `Object.prototype` versions of those functions. Ergo, string and primitive coercion are inherent object methods (the ones that get `[[DoubleSquareBrackets]]` in the ECMA standards). In Ruffle, our equivalent to `[[DoubleSquareBrackets]]` methods are methods on the `TObject` trait, so we're adding them there.

This mechanism will make implementing boxed value types (ala AVM1's `BoxedObject`) easier, too.

We also add some reasonable defaults for `ScriptObject` and `FunctionObject` which will appear on objects, functions, and classes.
2020-07-13 17:44:53 -04:00
David Wendt ba2c1f5750 Add test for `setPropertyIsEnumerable` 2020-07-13 17:44:39 -04:00
David Wendt f13e2ea3c4 Implement `setPropertyIsEnumerable` 2020-07-13 17:44:37 -04:00
David Wendt dc0cb00a03 Add a test for `propertyIsEnumerable`. 2020-07-13 17:44:37 -04:00
David Wendt 2afbcf450a Impl `propertyIsEnumerable` 2020-07-13 17:44:36 -04:00
David Wendt a0ca5891e4 Prevent instance traits from being accessible directly from prototypes. 2020-07-13 17:44:36 -04:00
David Wendt 307a95e5c4 `callproperty` and `callpropvoid` should *never* get callables from `base_proto`. 2020-07-13 17:44:35 -04:00
David Wendt 42cb8f57c8 Add a test for `has_own_property` in various class instance scenarios. 2020-07-13 17:44:35 -04:00
David Wendt 6e2508a79d Fix `any` name resolution, at least enough for the `has_own_property` test to work.
Private names now return `false`, and we run any names through trait lookups. This also means any namespace resolution can fail now, in case we need to throw a `VerifyError`.
2020-07-13 17:44:34 -04:00
David Wendt 8677804ea0 Actually enable the `isPrototypeOf` test. 2020-07-13 17:44:34 -04:00
David Wendt c6265bb50c Allow tracing booleans.
This requires implementing *some level* of coercions, even though this isn't the way to do it.
2020-07-13 17:44:34 -04:00
David Wendt 00186f7602 Free functions always have a `prototype`, this is a holdover from ES3. 2020-07-13 17:44:33 -04:00
David Wendt 0e89cb2175 Impl `Object.isPrototypeOf` w/ test 2020-07-13 17:44:33 -04:00
David Wendt d29f3dc1d0 Add `as3_object_enumeration` and `as3_class_enumeration` tests.
The former tests iterating normal objects and the latter tests iterating objects with prototypes.
2020-07-13 17:44:33 -04:00
David Wendt c014b40109 Implement `hasnext`, `hasnext2`, `nextname`, `nextvalue`, and the underlying enumeration machinery that powers it.
I have... significant reservations with the way object enumeration happens in AVM2. For comparison, AVM1 enumeration works like this: You enumerate the entire object at once, producing a list of property names, which are then pushed onto the stack after a sentinel value. This is a properly abstract way to handle property enumeration.

In AVM2, they completely replaced this with index-based enumeration. What this means is that you hand the object an index and it gives you back a name or value. There's also an instruction that will give you the next index in the object.

The only advantage I can think of is that it results in less stack manipulation if you want to bail out of iteration early. You just jump out of your loop and kill the registers you don't care about. The disadvantage is that it locks the object representation down pretty hard. They also screwed up the definition of `hasnext`, and thus the VM is stuck enumerating properties from 1. This is because `hasnext` and `hasnext2` increment the index value before checking the object. Code generated by Animate 2020 (which I suspect to be the final version of that software that generates AVM2 code) initializes the index at hero, and then does `hasnext2`, hence we have to start from one.

I actually cheated a little and added a separate `Vec` for storing enumerant names. I strongly suspect that Adobe's implementation has objects be inherently slot-oriented, and named properties are just hashmap lookups to slots. This would allow enumerating the slots to get names out of the object.
2020-07-13 17:44:32 -04:00
David Wendt 73189b6449 Properly unwind errors thrown from the AVM2 reader. 2020-07-13 17:44:32 -04:00
David Wendt 1cc8954747 Impl `pop`, which is the opposite of `dup`; and also the opposite of all the `push` instructions.
Confusingly, this one isn't documented in the AVM2 spec at all, but it's method of operation is fairly obvious.
2020-07-13 17:44:31 -04:00
David Wendt 9496fbde0a Remove `DontEnum`, `is_enumerable` and attribute mutation. They won't be needed. 2020-07-13 17:44:31 -04:00
David Wendt 67b7fbb593 Implement `label`, which is a no-op designed specifically to silence verifier errors about unreachable code. 2020-07-13 17:44:31 -04:00
David Wendt da6a7c0723 Implement `kill`, at least a little.
I'm sure there's some other part of AVM2 that cares about killed registers, but I couldn't find it yet.
2020-07-13 17:44:30 -04:00
David Wendt a44e700e81 Sign extend negative `s24` values correctly. 2020-07-13 17:44:30 -04:00
David Wendt 7253c091a2 Add tests for control flow instructions that use booleans or strict equality.
Other comparisons will have to wait until we have ECMA-compliant abstract comparison and coercion.
2020-07-13 17:44:30 -04:00
David Wendt 9c5ea1d30c Implement `jump`, `iftrue`, `iffalse`, `ifstricteq`, and `ifstrictne`. 2020-07-13 17:44:30 -04:00
David Wendt b33c246713 Implement `is_property_overwritable`. 2020-07-13 17:44:29 -04:00
David Wendt ddc9aa4cca Add a test for ES4 method binding of `this`. 2020-07-13 17:44:29 -04:00
David Wendt 915b2da42b Allow binding a reciever to a function, and make all method traits bind themselves to the object they were constructed on.
Our already odd `super` handling throws up another subtlety regarding bound recievers. Since we have to construct an instance of a parent class in order to get traits on it, we also have to make sure that we initialize traits with the correct reciever. I'll demonstrate here:

```let mut base = base_proto.construct(avm, context, &[])?;
let name = base.resolve_multiname(&multiname).unwrap();
let value = base.get_property(object, &name, avm, context)?.resolve(avm, context)?```

In this case, if `name` is the name of a method, getter, or setter trait, then `get_property` will instantiate that trait on `base` but bound to `reciever`. This is correct behavior for this case, but more generally, trait instantiation is permenant and therefore there's potential for confusing shenanigans if you `get_property` with the wrong reciever.

To be very clear, `reciever` should *always* be the same object that is getting `get_property` et. all called on it. In the event that you need to instantiate traits with a different `reciever`, you should construct a one-off object and retrieve prototypes from that.
2020-07-13 17:44:28 -04:00
David Wendt f042e453a3 Add a test for interactions between prototype and class-trait properties.
This is the test that broke the old object model's back, please see parent commit's description for more details.
2020-07-13 17:44:27 -04:00