Coder Social home page Coder Social logo

Comments (7)

justjake avatar justjake commented on June 9, 2024

This is allowed already although it’s not documented. Emscripten modules can have their WASM module overridden. You can try doing so by reading over our code in variants.ts and trying to see where you could inject the WASM module you import in your own code via import.

from quickjs-emscripten.

danthegoodman1 avatar danthegoodman1 commented on June 9, 2024

There are a few more issues to cloudflare workers to this that I've found following this

First you need to replace a ton of { credentials: "xxx" } fetch objects with {} because fetch does not support that in it's implementation on workers.

You also need to do some global and self overriding because self.href does not exist.

Here is my code so far:

import { newQuickJSAsyncWASMModule } from 'quickjs-emscripten'

export default {
	async fetch(
		request: Request,
		env: Env,
		ctx: ExecutionContext
	): Promise<Response> {
		(global as any).window = self as any;
		(self as any).location = { href: "/" } as any;

		const m = await newQuickJSAsyncWASMModule()
		const vm = m.newContext()

		const world = vm.newString("world")
		vm.setProp(vm.global, "NAME", world)
		world.dispose()

		let res
		const result = vm.evalCode(`"Hello " + NAME + "!"`)
		if (result.error) {
			console.log("Execution failed:", vm.dump(result.error))
			result.error.dispose()
		} else {
			res = vm.dump(result.value)
			console.log("Success:", res)
			result.value.dispose()
		}

		vm.dispose()

		return new Response("Hello World!");
	},
};

Making a request to this leaves with a massive b64 dump ending like:

FABBz6wECxUXAAAAABcAAAAACRQAAAAAABQAABQAQf2sBAsBFgBBia0ECycVAAAAABUAAAAACRYAAAAAABYAABYAADAxMjM0NTY3ODlBQkNERUYAQdStBAsBCQBB/K0ECwj//////////wBBxK4ECx0DAAAAAAAAAAIAAAAAAAAAAQAAAAEAAAABAAAABQBB7K4ECwJ2AQBBhK8ECwt3AQAAeAEAAJgcAQBBnK8ECwECAEGsrwQLCP//////////AEHwrwQLCWAXAQAAAAAABQBBhLAECwJ5AQBBnLAECw53AQAAegEAAKgcAQAABABBtLAECwEBAEHEsAQLBf////8KAEGIsQQLB/gXAQCgIlE=";if(!V.startsWith(U)){var
  xa=V;V=a.locateFile?a.locateFile(xa,v):v+xa}function ya(){var b=V;try{if(b==V&&H)return new
  Uint8Array(H);var c=D(b);if(c)return c;if(y)return y(b);throw"both async and sync fetching of
  the wasm failed";}catch(d){G(d)}}
                                                                         ^
      at G
  (/Users/dangoodman/code/test-quickjs-worker/node_modules/quickjs-emscripten/ts/generated/emscripten-module.WASM_RELEASE_ASYNCIFY.js:21:71)
      at
  (/Users/dangoodman/code/test-quickjs-worker/node_modules/quickjs-emscripten/ts/generated/emscripten-module.WASM_RELEASE_ASYNCIFY.js:36:414)


✘ [ERROR] Uncaught (in response) CompileError: Aborted(CompileError: WebAssembly.instantiate(): Wasm code generation disallowed by embedder). Build with -s ASSERTIONS=1 for more info.

Possibly newQuickJSAsyncWASMModule() is not the correct one I was supposed to use from the variants.ts file?

from quickjs-emscripten.

danthegoodman1 avatar danthegoodman1 commented on June 9, 2024

I have also noticed there is a significant delay in request processing before this error throws, maybe about a second

from quickjs-emscripten.

Hades32 avatar Hades32 commented on June 9, 2024

Thanks Jake, I'll look into that.

Dan, I think the fetch code won't be used when the module is loaded via an import

from quickjs-emscripten.

justjake avatar justjake commented on June 9, 2024

@Hades32 @danthegoodman1 I think the changes in 0.25.0 should make it much easier to get quickjs-emscripten in Cloudflare Workers.

from quickjs-emscripten.

justjake avatar justjake commented on June 9, 2024

I tried it out, and there's a bunch of challenges.

from quickjs-emscripten.

justjake avatar justjake commented on June 9, 2024

I just release v0.26.0, which supports Cloudflare Workers with some setup. See the instructions here: https://github.com/justjake/quickjs-emscripten#webassembly-loading

from quickjs-emscripten.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.