Commit Graph

2661 Commits

Author SHA1 Message Date
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
David Wendt 2f95a7a81b Completely overhaul the way traits are defined on objects.
Previously, we were treating ES4 classes like syntactic sugar over a prototype chain (like ES6 classes); e.g. each declared trait was set in the given prototype and then property look-ups happened as normal.

This already caused problems with virtual properties, which could be partially-defined in subclasses and required careful checks to make sure we stopped checking the prototype chain on the *correct* half of the property.

However, this is a hint of a larger problem, which is that ES4 classes don't actually define anything on the prototype chain. Instead, the instance itself constructs class properties and methods on itself. This allows things like methods automatically binding `this`, which isn't included in this commit but will be implemented really soon.

The prototype chain still exists even on pure ES4 classes, due to the need for backwards compatibility with ES3 code. Object, for example, still defines it's methods as prototype methods and thus there needs to be a prototype chain to reach them. I actually could have gotten away with using the prototype chain if AS3 *hadn't* retained this "legacy" detail of ES3 allowing this class/prototype distinction to leak out into upcoming tests.

We still actually use the prototype chain for one other thing: trait resolution. When we look for a trait to install onto an object, we pull traits from the prototype chain using a special set of `TObject` methods. This happens in opposite order from normal prototype lookups so that subclassing and verification can proceed correctly.

`super` somehow became even harder to implement: we now actually construct the parent class so we can get traits from it, which is going to complicate method binding as mentioned above.
2020-07-13 17:44:27 -04:00
David Wendt 353017576a `ScriptObject` now holds a reference to a class and allows retrieving traits from it. 2020-07-13 17:44:27 -04:00
David Wendt f10920adc0 Implement `Object.prototype.hasOwnProperty` and resolution of `Namespace::Any`. 2020-07-13 17:44:26 -04:00
David Wendt 67744650f1 Pass the ABC name and lazy init flag to the AVM2. 2020-07-13 17:44:25 -04:00
David Wendt 6cc3f7ecc3 Add a test for stored properties as well.
This test passed with no errors.
2020-07-13 17:44:24 -04:00
David Wendt 5abc78d3bd Add test of AVM2 virtual properties.
This tests:

 * Getter invocation
 * Setter invocation
 * Properties with one or the other, but not both
 * Inheritance
 * Superproperty getters and setters
 * Getters with inherited setter
 * Setters with inherited getter
2020-07-13 17:44:24 -04:00
David Wendt c5e3af2053 When resolving `get_property`, skip over virtual properties that do not have a defined getter. 2020-07-13 17:44:23 -04:00
David Wendt 54b792ef3a Ensure that called setters are properly resolved so that errors in setters propagate up the Rust stack correctly.
The previous system for handling setters would execute the setter and then return a value to indicate whether or not the caller needed to resolve a stack frame. However, no caller of `Property.set` actually did this. Ergo, errors in setters and getters would not resolve up the stack at the correct time.

This problem also exists in AVM1 but is far less noticable as AVM1 only has two very uncommon runtime errors and very few movies use `throw`.
2020-07-13 17:44:23 -04:00
David Wendt b8106d24d2 Ensure virtual setters are run when defined on a prototype.
Normally, `set_property` only affects the object it was called on, which makes sense: otherwise, we couldn't override values that originate from a class prototype without accidentally monkey-patching the prototype. However, virtual setters only exist in prototypes and need to be accessible from child objects.

The solution to this is to have a specific method to check if a virtual setter exists. Virtual setters are then resolved through the prototype chain. If no virtual setter exists, then the reciever object is handed the value.

Note that we always use the `reciever` object rather than `self` so that `setsuper` can work correctly. In `setsuper`, we resolve the base class, and then set properties on it with the actual object in question as it's reciever. If a virtual setter is called, it will get the actual object it should be manipulating; and otherwise, prototypes will not be modified or consulted.
2020-07-13 17:44:22 -04:00