#Deno Runtime Extension with JS File

67 messages · Page 1 of 1 (latest)

peak grotto
#

When i include js files with

Extension::builder()
  .js(include_js_files!(prefix "prefix", "file.js")
  .build()

Do i need to call these js files somehow? Can't find any reference in the source code, would really appreciate your help 😄

ashen axle
#

those will be ran at start up before the user code is ran iirc

peak grotto
#

Doesn't look like it is doing anything with it. Tried console.log, deno.core.print, also tried calling something that doesn't exist to provoke a error from the runtime... but nothing... as if it would be ignored

ashen axle
#

is it bound to any object in javascript like on the GlobalThis etc

peak grotto
#

I think so, i tried this

"use strict";

((window) => {
  // then do stuff here like
  Deno.exit(-1);
  // or
  somethingThatDoesNotExist();
})(globalThis);

Like in other scripts from deno, but nothing happens

ashen axle
#

Is that script you are adding via include file?

#

I believe your extension ops are bound on Deno.core.ops and then you should bind your js glue code via the following code

((window) => {
  const ops = window.Deno.core.ops;

  function yourFunc() {
    ops.your_op_call();
  }

  window.yourFunc = yourFunc;
})(globalThis);
ashen axle
peak grotto
ashen axle
#

weird

#

what is undefined btw

#

the op or the function you expose

peak grotto
#

The function

#

yourFunc is not defined

#

When i use Deno.core.ops.... directly it works

ashen axle
#

can you call the IIFE with (this) instead of globalThis

#
((window) => {
  const ops = window.Deno.core.ops;

  function yourFunc() {
    ops.your_op_call();
  }

  window.yourFunc = yourFunc;
})(this);
#

Like this

peak grotto
#

I'll try

#

Same result

ashen axle
#

what...

#

Then idk. It should work just like this?

peak grotto
#

Maybe i did something wrong with bootstrapping and running:

fn create_main_worker() -> Result<MainWorker, Error> {
    let mut worker = MainWorker::bootstrap_from_options(
        deno_core::resolve_url_or_path(deno_core::DUMMY_SPECIFIER)?,
        Permissions::allow_all(),
        WorkerOptions {
            extensions: vec![
                Extension::builder()
                    .ops(vec![
                        op_get_header::decl()
                    ])
                    .js(include_js_files!(
                        prefix "prefix:idk",
                        "js/01_helpers.js",
                    ))
                    .build()
            ],
            ..Default::default()
        },
    );

    Ok(worker)
}

async fn run_worker() -> Result<(), Error> {
    let mut runtime = create_main_worker()?;
    let module = deno_core::resolve_url_or_path("bundle.js")?;

    runtime.execute_main_module(&module).await?;
    runtime.run_event_loop(false).await?;

    Ok(())
}

Also debugged it, and the script that is embedded is correct

#

Maybe because of the "dummy specifier"?

ashen axle
#

you mean the "prefix:idk"

#

I don't think that matters

peak grotto
#

i mean that i bootstrap the runtime with deno_core::DUMMY_SPECIFIER instead of the real script

ashen axle
#

I don't think that should matter? not sure tho

peak grotto
#

I will try with the real script

#

Still undefined

snow moon
#

If you don't use snapshots you need to pass it as extensions_with_js

peak grotto
snow moon
#

errr, good question

#

@hearty ocean ?

hearty ocean
#

uhh, lemme check

#

i didnt follow the entire discussion, but @peak grotto are you using deno_core or deno_runtime?

peak grotto
#

deno_runtime

#

And MainWorker doesn't let you use extensions_with_js

#

Or at least, i couldn't see an option for this

hearty ocean
#

@peak grotto from the looks of it you need to pass it to extensions then

peak grotto
hearty ocean
#

hmm

peak grotto
#

Is there any workaround maybe?

hearty ocean
#

are you using snapshot from snapshot?

peak grotto
#

As far as i know i dont use any snapshots

hearty ocean
#

hmmm

#

what happens if you try to use an older deno_runtime version (ie 0.80.0)?

peak grotto
#

Can't i use #[op] without depending on deno_core when deno_runtime is present? I don't know which version of deno_core i should use when using deno_runtime 0.80.0

hearty ocean
#

deno_runtime re-exports deno_core

peak grotto
#

Yeah but when not having deno_core in cargo.toml #[op] tells me: deno_core not present in Cargo.toml: CrateNotFound

hearty ocean
#

huh 🤔 thats annoying

#

lemme get you the right core version

#

0.154.0

peak grotto
peak grotto
peak grotto
hearty ocean
#

hm ok looking into it

peak grotto
#

Thanks 🙂

peak grotto
#

Could MainWorker not just expose the extensions_with_js Option?

peak grotto
#

Nice

peak grotto
hearty ocean
#

Can you tell when I can expect this to be merged/released (i do see some failed pipelines)?
possibly next week

#

Also, should i open an issue about #[op] requiring deno_core to be in the cargo.toml, even if deno_runtime is present?
cc @strong tundra