Skip to content
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

Update JS API #379

Merged
merged 10 commits into from
Jun 15, 2023
Merged

Update JS API #379

merged 10 commits into from
Jun 15, 2023

Conversation

rossberg
Copy link
Member

Adjust JS API to match core spec:

  • Distinguish ref.host and ref.extern and change conversion functions accordingly.
  • Add previously missing type checks to ToWebAssemblyValue.
  • Remove in/externalize functions, which are not actually needed.

@rossberg rossberg requested a review from tlively May 17, 2023 11:34
@rossberg rossberg mentioned this pull request May 17, 2023
53 tasks
@@ -1149,27 +1143,42 @@ The algorithm <dfn>ToWebAssemblyValue</dfn>(|v|, |type|) coerces a JavaScript va
1. Return [=ref.null=] |heaptype|.
1. Otherwise,
1. Throw a {{TypeError}}.
1. If |type| is a subtype of [=ref=] |null| [=heap-type/func=],
1. If |heaptype| is a subtype of [=heap-type/func=],
Copy link
Member

Choose a reason for hiding this comment

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

Should we use the "matches" terminology to match the core spec?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

1. Let |objectaddr| be the value of |v|'s \[[ObjectAddress]] internal slot.
1. Let |objecttype| be the value of |v|'s \[[ObjectType]] internal slot.
1. If |objecttype| is "array",
1. If |heaptype| is not a supertype of [=array=],
Copy link
Member

Choose a reason for hiding this comment

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

Supertype or subtype, right? Since we want to do implicit downcasts on the boundary. Same for structs below.

We also need to handle the possibility of the downcast failing for structs, arrays, and functions.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the check for the downcast. The heaptype is the target type here, so the actual type must be a subtype of it for that cast to succeed.

Using "matching" terminology now, which may make this somewhat clearer.

1. If |heaptype| is not a supertype of [=struct=],
1. Throw a {{TypeError}}.
1. Return [=ref.struct=] |objectaddr|.
1. If |heaptype| is not a supertype of [=any=],
Copy link
Member

Choose a reason for hiding this comment

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

It would be clearer to use (in)equality here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Turned around now using matching terminology. Not using inequality mainly for symmetry with the other checks.

@rossberg
Copy link
Member Author

@tlively, PTAL.

1. If |objecttype| is "array",
1. If [=array=] does not match |heaptype|,
1. Throw a {{TypeError}}.
1. Return [=ref.array=] |objectaddr|.
Copy link
Member

Choose a reason for hiding this comment

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

Don't we still need to do a ref.test to make sure the downcast succeeds when |heaptype| is a specific array type? Same with structs below.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, we do not support concrete array types in the API yet, so this assumes the invariant that heaptype is one of the abstract heap types. Should we make this more powerful?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, am I misremembering the result of those discussions? I thought we allowed arbitrary types on the boundary.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think I was correct: #279 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Or maybe I misremember -- I assumed concrete types cannot come up because we cannot express them API-side. But did we decide to allow calling into exported Wasm functions with concrete types?

I guess there is nothing else preventing it anyway, so something needs to be fixed here. I'll extend the semantics. However, this will probably require replacing the existing [[ObjectKind]] thingy with a proper call to the embedder interface (to get the precise object type), which I haven't written yet.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, right, we don't have a way to describe concrete types from the JS API, but yes, I was thinking about the case of calling into an exported function (or setting an exported global, etc) that expects a concrete type.

Copy link
Member

Choose a reason for hiding this comment

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

Can't we just execute a ref.test like we do for function types?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah yes, you are right, I already added that as part of the funcref proposal.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, actually I didn't. So that probably is a dangling ref at the moment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, I think this is fixed now. I also refactored the function to have less duplication.

1. If |v| is an [=Exported GC Object=],
1. Let |objectaddr| be the value of |v|'s \[[ObjectAddress]] internal slot.
1. Let |objectkind| be the value of |v|'s \[[ObjectKind]] internal slot.
1. If |objecttype| is "array",
Copy link
Contributor

@takikawa takikawa Jun 13, 2023

Choose a reason for hiding this comment

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

I think there's a typo here, probably my fault as I think it's in the old revision too (where I forgot to rename to ObjectKind at all spots). Should be |objectkind| instead of |objecttype|?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, fixed.

Comment on lines 1169 to 1170
1. If [=match_valtype=](|type|, [=ref=] |null| [=heap-type/func=]) is false,
1. Throw a {{TypeError}}.
Copy link
Member

Choose a reason for hiding this comment

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

Is this supposed to use actualtype somewhere? Also, why is the store involved?

Copy link
Member Author

@rossberg rossberg Jun 14, 2023

Choose a reason for hiding this comment

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

Oops, yes, forgot to update that line after copy & paste. Fixed.

The store is needed to obtain the precise type of a reference value, because it can't be told from the address alone.

Copy link
Member

@tlively tlively left a comment

Choose a reason for hiding this comment

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

LGTM!

@rossberg rossberg merged commit fb77043 into main Jun 15, 2023
@rossberg rossberg deleted the spec.js branch June 15, 2023 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants