zenscript icon indicating copy to clipboard operation
zenscript copied to clipboard

Foreign Function Import

Open keean opened this issue 8 years ago • 6 comments

This issue is for discussing the mechanism that will be used for interfacing and importing foreign functions.

keean avatar Oct 04 '16 14:10 keean

I don't understand. You have to have a way to lift types and functions out of Javascript into Zenscript. There are only two realistic options:

Provide binding syntax. I explained above it is useful to hand craft bindings. This does not preclude auto generation of the bindings. For Felix I autogenerated bindings for some huge libraries. And then spent weeks fixing them. The binding constructions have to part of the language.

Allow naked JS. I don't think this is what you want. If there's another option, I don't know what it is.

The other option is to have the type system span both languages. So we can directly give types to some JavaScript functions. Then we just import them and give a type signature.

keean avatar Oct 04 '16 14:10 keean

I an going to start with this simple syntax:

foreign console.log(String) : ()

To bring the foreign function into scope with the specified type.

keean avatar Oct 04 '16 17:10 keean

@keean wrote:

So we can directly give types to some JavaScript functions.

The type of JavaScript stuff is always any and thus we can't do anything with it in our type system unless we are just writing and not reading. For reading in values, we must have wrappers that actually check values at runtime and enforce the types it assigns to them.

Also on writing we may need to do conversion for example to Number, unless we have a Number type in our language which models the JavaScript type.

shelby3 avatar Oct 04 '16 17:10 shelby3

The type of JavaScript stuff is always any and thus we can't do anything with it in our type system. We have to have wrappers that actually check values at runtime and enforce the types it assigns to them.

Not when you are calling out. Calling JS from Zen we control the types that are given to the function, so if you declare colsole.log takes a String, you will only be able to pass a string to it.

For return values we would give them a datatype with runtime tags, so they would get assigned into a disjoint union and you can match on the type in Zen.

I would have a builtin datatype like this:

data JSType = Undefined | Null | Boolean(Boolean) | Number(Float) | String(String) | Symbol(String)

Function types and object types will need a bit more thought.

keean avatar Oct 04 '16 18:10 keean

Are we sure a JavaScript Number is equivalent to the native Float type on CPUs? I assume we want to use a native type?

shelby3 avatar Oct 05 '16 02:10 shelby3

@shelby3 it might be a double, I will check, but the principle is this will let us write any wrapper code for native inside our language where things are type safe.

keean avatar Oct 05 '16 05:10 keean