Javet icon indicating copy to clipboard operation
Javet copied to clipboard

Preload script for later use

Open carneirocarlos opened this issue 1 year ago • 3 comments

Is there a way to pre-load a script for later use?

Imagine that I have script A and script B, script A never changes but script B is dynamic, is there a way to preload script A inside an executor and execute script B in the same executor that script A is preloaded?

carneirocarlos avatar Jul 27 '22 19:07 carneirocarlos

I think ESM might be the one you are looking for.

Javet allows the applications to load ES modules via import {...} from 'module'. Those modules can be actively cached by V8 in a lazy-loading manner, or if you want to, you may have the applications preload them.

Please refer to this doc for detail.

caoccao avatar Jul 28 '22 00:07 caoccao

Thanks @caoccao , the ESM would work but unfortunately the script is not ES Module compatible.

Are values assigned to the GlobalObject immediately copied and cached inside the runtime?

this.runtime.getGlobalObject().set("staticScript", script);

carneirocarlos avatar Jul 28 '22 01:07 carneirocarlos

You cannot set a script as a string literal directly, hoping it works. Because it remains a string in V8. You have to set a function which has already been compiled from a script string.

If you set a function to any objects including the global object, yes, it becomes effective immediately.

I suggest you to inject a proxied Java object if that's possible with the following benefits.

  • No compilation
  • No additional memory
  • Super fast

caoccao avatar Jul 28 '22 01:07 caoccao