Skip to content

avm2: Add #[native] macro to allow defining methods with native types #7895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Aaron1011
Copy link
Member

@Aaron1011 Aaron1011 commented Sep 6, 2022

This allows you to define a native method like:

#[native]
fn my_method(activation: &mut Activation, this: DisplayObject<'gc>, arg:
bool)

instead of needing to use Option<Object<'gc>> and &[Value<'gc>]
and manual coercions.

See the doc comments for more details.

@Aaron1011 Aaron1011 force-pushed the min-native-macro branch 3 times, most recently from d2b0087 to 6c650d2 Compare September 6, 2022 01:45
@relrelb
Copy link
Contributor

relrelb commented Sep 6, 2022

I wonder why not implement this instead in build_playerglobal, in the same manner of nativegen.py in avmplus - Each native function will have a generated Rust wrapper function that coerces this: Option<Object<'gc>> and args: &[Value<'gc>] to the appropriate parameters, then calls the actual implementation. This way we'll have access to ActionScript's type system while generating the wrappers, which I guess might help at producing better conversions.

@adrian17
Copy link
Collaborator

adrian17 commented Sep 7, 2022

FWIW I agree; "rust-dependent" paths could be handled by Into impl, and/or by extra annotations on the AS side (stripped after codegen).
Talked about it a bit on Discord too; some of the current type checking is also redundant after calling resolve_parameters().

Copy link
Member

@Herschel Herschel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I'm AFK this weekend so am not able to test this deeply, but it looks fine at a glance, so please feel free to merge if you feel its ready -- otherwise I'll get to it on Monday.

Re: redundant checks in extract_from_vm, I think this will be cleaned up when we make a smarter Value type (union, tagged pointer, NaN-boxed, whatever), which will let us easily do the get_unchecked equivalent inside extract_from_vm. In the meantime, maybe std::hint::unreachable_unchecked() could let us extract the enum variants in a branchless fashion?

Copy link
Member

@kmeisthax kmeisthax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this has been sitting on the PR queue for far too long; mind giving it one more rebase/merge resolution cycle?

@Herschel
Copy link
Member

Sorry that I didn't merge this way back! I agree, assuming this is still useful, let's rebase this.

@Aaron1011 Aaron1011 force-pushed the min-native-macro branch 4 times, most recently from 76322ef to db87295 Compare December 22, 2022 20:56
@Aaron1011
Copy link
Member Author

@Herschel @kmeisthax rebased

@Bale001
Copy link
Member

Bale001 commented Dec 23, 2022

One thing I'm worried about is whether or not "extracting" the type directly is the right approach. From my perspective, it makes more sense to coerce the value to the specified type instead.

For example, currently if I have a parameter like my_param: f64, it will fail if I pass a Value::Integer as that parameter. It will also fail if I pass a string that contains a number, like "0". I do not see this behavior if Flash though, it appears to always coerce the value, even for built-in methods.

@adrian17
Copy link
Collaborator

adrian17 commented Dec 23, 2022

my_param: f64, it will fail if I pass a Value::Integer as that parameter

Note that Number/int/uint are tricky in general. Just check out Value::is_number, Value::is_u32 etc.
But also... the thing is, the interpreter already does these coercions, in resolve_parameters() (assuming the method is correctly typed in AS). So we don't want to duplicate work, in fact ideally I'd love to be able to unsafe-coerce the type to say String in Rust if we can guarantee that the AS typing already coerced it to a String. But again, for numbers this is tricky as type annotation x: Number will still accept either an integer of float Value - the spot to convert the Value to a concrete number type must be on Rust side.

@Aaron1011
Copy link
Member Author

I believe we already do those coercions (based on the type declared in the signature) in the Activation code that sets up a method call.

@adrian17
Copy link
Collaborator

adrian17 commented Dec 23, 2022

I believe we already do those coercions (based on the type declared in the signature) in the Activation code that sets up a method call.

More specifically, for numbers, (even if Ruffle doesn't do it perfectly in line with FP, eventually we should), this is still awkward:

function f(x: Number) {
	trace(getQualifiedClassName(x));
}

var a = f;
a(1);    // int
a(1.5);  // Number
a("12"); // int

So even if you want an f64 in rust, even after AVM-layer coercions, you still need to handle both Value::Integer and Value::Number.

@Aaron1011 Aaron1011 force-pushed the min-native-macro branch 2 times, most recently from 49d4e76 to e6d0325 Compare January 4, 2023 17:09
@Dinnerbone
Copy link
Contributor

@Aaron1011 wanna give it one last rebase and see if we can finally land this?

@Aaron1011 Aaron1011 force-pushed the min-native-macro branch 2 times, most recently from 6db3d2e to 0dffde1 Compare January 10, 2023 22:12
This allows you to define a native method like:

```rust
fn my_method(activation: &mut Activation, this: DisplayObject<'gc>, arg:
bool)
```

instead of needing to use `Option<Object<'gc>>` and `&[Value<'gc>]`
and manual coercions.

See the doc comments for more details.
@Aaron1011
Copy link
Member Author

So even if you want an f64 in rust, even after AVM-layer coercions, you still need to handle both Value::Integer and Value::Number.

I think the vast majority of native methods will always be coercing to some internal type like Twips, so this won't come up very often. When it does, the native method can just use Valuefor the affected parameter.

@danielhjacobs danielhjacobs added the T-refactor Type: Refactor / Cleanup label Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-avm2 Area: AVM2 (ActionScript 3) T-refactor Type: Refactor / Cleanup
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants