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

JS backend should be more liberal about typing #668

Open
Manishearth opened this issue Sep 2, 2024 · 8 comments
Open

JS backend should be more liberal about typing #668

Manishearth opened this issue Sep 2, 2024 · 8 comments
Labels
B-js JS backend

Comments

@Manishearth
Copy link
Contributor

I think the JS backend should accept objects with the right fields in lieu of struct types, and strings in lieu of enum types.

Basically, if a method method accepts struct Struct {a: Enum, b: u32} where Enum has variants Foo, Bar, it should also be acceptable to invoke it with method({a: "foo", b: 1}).

The easiest way to do this is probabyl give every enum and non-out struct a constructor that acceps either the looser typed version, or an instance of itself, and unconditionally call the ctor before doing any JS-to-C shenanigans

@Manishearth
Copy link
Contributor Author

Similarly, we need toString on all enums

@Manishearth Manishearth added the B-js JS backend label Sep 2, 2024
@Manishearth
Copy link
Contributor Author

cc @ambiguousname UX of the JS API is probably an interesting thing to hack on, and probably not too involved.

Writing tests is a good way to discover these UX pitfalls, too.

@ambiguousname
Copy link
Member

ambiguousname commented Sep 2, 2024

That would be interesting! I agree, I think this would be fairly easy to set up in the JS API. I really like the idea of checking an object for the right fields inside of our methods instead of being so strict about what we pass in. That would allow us to remove some of the more clunky elements of the API (like _intoFFI).

And yes, lots of tests would be great.

(Edit: Manish's comment below is right, _intoFFI is still a necessity)

@Manishearth
Copy link
Contributor Author

at would allow us to remove some of the more clunky elements of the API (like _intoFFI).

we still would need _intoFFI i think!

@Manishearth
Copy link
Contributor Author

I think no matter what once we have a bag of stuff we need some way to share code on how that bag of stuff gets converted to something FFI-compatible. I think _intoFFI()-style methods are actually good for this kind of thing: we always need to turn whatever freeform type we have into an object of the right concrete type (which we can teach the ctor to do, or potentially some static method), and then that type has a convenient method.

@ambiguousname
Copy link
Member

Mostly I'm annoyed at the _ in the function, The pseudo-privateness feels really wrong to me. If we made it a static function (static intoFFI()), then that would give us wiggle room to allow any object passed to us with the right members.

@ambiguousname
Copy link
Member

ambiguousname commented Sep 2, 2024

It also gives the added benefit of cleaning up intoFFI, as we'd need to check for the existence of each member in the object. Then that makes it super easy to read what intoFFI does. For instance, something like:

static intoFFI(object) {
  var array = [];
  if ("a" in object) {
    array.push(diplomatRuntime.someConversionFunction(object.a));
  } else {
    throw new Error("...");
  }
  return array;
}

@Manishearth
Copy link
Contributor Author

Mostly I'm annoyed at the _ in the function, The pseudo-privateness feels really wrong to me. If we made it a static function (static intoFFI()), then that would give us wiggle room to allow any object passed to us with the right members.

I think we can make it properly private via a symbol, it should be [diplomatRuntime.intoFFI]: instead of intoFFI().

I still think it should be a method, if we want a higher level constructFromAnything() we can make it too. I think that method should just be the constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-js JS backend
Projects
None yet
Development

No branches or pull requests

2 participants