keean / zenscript

A trait based language that compiles to JavaScript
MIT License
42 stars 7 forks source link

Foreign Function Import #25

Open keean opened 7 years ago

keean commented 7 years ago

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

keean commented 7 years ago

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 commented 7 years ago

I an going to start with this simple syntax:

foreign console.log(String) : ()

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

shelby3 commented 7 years ago

@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.

keean commented 7 years ago

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.

shelby3 commented 7 years ago

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

keean commented 7 years ago

@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.