silverbullet icon indicating copy to clipboard operation
silverbullet copied to clipboard

[Plugs] Expose a syscall to register a new function

Open zefhemel opened this issue 1 year ago • 2 comments

You can currently only define custom functions (https://silverbullet.md/Functions) in space script, not through plugs. This is a bit silly and should be made possible.

Likely design:

system.registerFunction({name: "myFunction"}, `() => 10`);

The second argument will need to be a string with JavaScript code (to be eval'ed) because custom functions must (for performance reasons) run in the main browser thread. So we need to ship the code from a plug over to the main thread in JavaScript text form, unfortunately.

zefhemel avatar Jul 31 '24 10:07 zefhemel

This would be a great addition!

You might be able to convert a function to a string and then eval it later? It'd need some testing, but looks like it might be possible: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString

justyns avatar Aug 02 '24 05:08 justyns

You can, but you'd lose regular JavaScript semantics like closures (referencing variables outside the function). So I'd like to avoid weird surprises like that.

zefhemel avatar Aug 02 '24 05:08 zefhemel

No longer relevant

zefhemel avatar Jun 04 '25 17:06 zefhemel