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

foreign function interface in KRL so that rulesets can use JavaScript modules #617

Open
b1conrad opened this issue Oct 20, 2022 · 2 comments

Comments

@b1conrad
Copy link
Member

Phil
Today at 9:33 AM
Idea: foreign function interface in KRL so that rulesets can use JavaScript modules. Any exported function world be importable. (edited)
👍
1

3 replies

Phil
32 minutes ago
One question would be how the module (and its dependencies) would be loaded into the engine so it’s available.

Phil
31 minutes ago
This would allow mixed programs of KRL and JS and allow KRL programs to make use of existing JS modules without having to modify the engine to load them.

Phil
24 minutes ago
JS imports can be urls. So we could limit modules to only those that load dependencies from urls. But I’m not sure how we do that to arbitrary depth. I guess then it’s the developers problem. We just report that a module can’t be loaded because dependencies can’t be found.

@b1conrad
Copy link
Member Author

b1conrad commented Oct 20, 2022

Currently, we write JS modules (aka libraries) in TypeScript, in the folder pico-engine/packages/pico-engine-core/src/modules and they get transpiled to JS and put into the pico-engine/packages/pico-engine-core/dist/modules folder.

However, that is not the end of the story. It is also necessary to modify pico-engine/packages/pico-engine-core/src/PicoEngineCore.ts to actually import the JS module and make it available to compiled (into JS) KRL rulesets. This commit shows the changes needed for a new JS module.

Things are tricky if the JS module has to have access to the pico engine core. It's a lot simpler if it just stands alone relative to pico things, as in the comment to this commit.

@b1conrad
Copy link
Member Author

b1conrad commented Oct 20, 2022

I'm imagining a directive in the meta block of a ruleset like:

  use module "URL.js" alias myJSfncs

which would allow function calls like

    sum = myJSfncs:add(1,2,3)

In order for the JS module to declare functions callable by the KRL code in the importing ruleset, it will have to import the krl-stdlib as in this line.

The current version of ursa.ts provides an example of how the coding would have to look. Search for the string "krl" to see uses of krl-stdlib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant