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

Cannot importScripts from Web Worker #44

Open
Castux opened this issue Mar 31, 2020 · 7 comments
Open

Cannot importScripts from Web Worker #44

Castux opened this issue Mar 31, 2020 · 7 comments

Comments

@Castux
Copy link

Castux commented Mar 31, 2020

Is it possible to run Fengari from a Web Worker? Naively trying importScripts("fengari-web.js"); gives the following error:

fengari-web.js:1 Uncaught ReferenceError: window is not defined
    at fengari-web.js:1
    at worker.js:1

Since the library is minified, it's hard to know what's going on, although the source for fengari-web.js seems to have a section that detects if there is a document, which suggests it might work without one?

@daurnimator
Copy link
Member

daurnimator commented Apr 5, 2020

It should be possible.... are you able to try again with an unminified version?

I guess the if (window.dispatchEvent(e)) { line is causing the reference error?
Is that happening at load time or is it actually reaching that code at runtime?
If it is reaching it at runtime, then you must have a document object in the web worker: is that normal/expected?

@Castux
Copy link
Author

Castux commented Apr 5, 2020

I'm afraid I'm not very proficient in JS (hence my interest in running Lua in the browser ;) ). I couldn't find a non minified release, and npm build minifies automatically.

My test is a barebones HTML document with the following script:

var w = new Worker("worker.js");

And worker.js is:

importScripts("fengari-web.js")
const L = fengari.lauxlib.luaL_newstate();

At page load, the error mentioned above is thrown:

fengari-web.js:1 Uncaught ReferenceError: window is not defined
    at fengari-web.js:1
    at worker.js:1

(Google Chrome Version 80.0.3987.163 (Official Build) (64-bit) on Windows 10, if that's relevant)

@Castux
Copy link
Author

Castux commented Apr 5, 2020

Ha! I've no idea what I'm doing, but in webpack.config.js I changed the first target to webworker, and the fengari-web.js that was produced with npm run build now seems to load properly inside a WebWorker. I only tested with console.log(fengari.load('return 1+1')()) but that did what it should, so it sounds promising.

@Castux
Copy link
Author

Castux commented Apr 8, 2020

Ok, this new way to build works (at least for my use: https://snake-eyes.io/), so the issue can be closed as far as I'm concerned. You can be the judge of whether there's a bug to fix, or a new target to add to the build system, etc.

@d0sboots
Copy link

I have the same issue. Same line numbers in the backtrace, etc., except I'm not using webpack or anything - just a raw download of fengari-web.js that I'm loading with importScripts().

The line that's failing is the following:

!function(t, e) {
    "object" == typeof exports && "object" == typeof module ? module.exports = e() : "function" == typeof define && define.amd ? define([], e) : "object" == typeof exports ? exports.fengari = e() : t.fengari = e()
}(window, function() {

It chokes on the "window". I don't see how it could ever work in a WebWorker, because there's no conditional there - the symbol will always be evaluated. Also, none of this appears in the original code - it appears to be coming from whatever minifier you're using.

@daurnimator
Copy link
Member

hmm, indeed the pre-compiled script contains the same thing. I guess we need to change some minifier setting

@d0sboots
Copy link

I did the same thing as Castux: changed the webpack config target from "web" to "webworker", and that fixed it for me too. Looking at the compiled output, the only diff, across the entire bundle, is literally changing the single "window" that it's choking on to "self".

I'm attaching the result for anyone else who has the same issue. However, judging from the result, I'd say it should be safe to change the main config to "webworker" for everyone. ("self" works portably in both contexts, because that's what it was designed for.)

fengari-web.zip

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

No branches or pull requests

3 participants