Skip to content

be interactive #4

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

Open
mafintosh opened this issue Oct 29, 2015 · 9 comments
Open

be interactive #4

mafintosh opened this issue Oct 29, 2015 · 9 comments

Comments

@mafintosh
Copy link

would be nice if this could execute the js as you stream it to it instead of buffering the entire source.
it could probably be executed like this

try {
  eval(sourceBuffer)
} catch (e) {
  if (e.name === 'SyntaxError') {
    // bufferMore
    return
  } else {
    throw err 
  }
}
sourceBuffer = ''
@thlorenz
Copy link

For that snippetify may help which will try to detect executable JS snippets in a given string.

@juliangruber
Copy link
Owner

great suggestion! i'm assuming snippetify will help with raw streaming data as chunk boundaries likely won't line up with line endings. i'll give it a shot!

@mafintosh
Copy link
Author

@thlorenz @juliangruber just used snippetify to make a streaming snippet parser, https://github.com/mafintosh/snippet-stream

then its as simple as

var snippets = require('snippet-stream')

// create a snippet stream
var stream = snippets()
var scope = {}

// write some js to it
stream.write('var a = 1\n')
stream.write('function foo () {\n')
stream.write(' return a + 1\n')
stream.write('}\n')
stream.write('console.log(foo())')
stream.end()

stream.on('data', function (data) {
  eval.call(scope, data)
})

@juliangruber
Copy link
Owner

@mafintosh ok that's really sweet. Ok I might need your guys' help again:

In the current design I boot an http server that serves the script inlined into a script tag: https://github.com/juliangruber/electron-stream/blob/master/index.js#L77. I'm doing this because otherwise window.onerror will only get "Script Error." instead of a full error with stack trace.

Could you think of a way to serve javascript via a snippet stream, like using electron's ipc module, while still retaining sourcemap and full window.onerror support?

@RangerMauve
Copy link

If you use an HTTP server you can use the response stream and pipe data into it

res.write("<!DOCTYPE html><title>electron-stream</title><script>");
js.pipe(res);
js.on("end", function(){ res.end("</script>"});

IIRC the browser will evaluate the JS as it's piped into the script tag.

Might need to do something about a keepalive, though.

@juliangruber
Copy link
Owner

juliangruber commented Sep 28, 2016

IIRC the browser will evaluate the JS as it's piped into the script tag.

I did a test on this and unfortunately no it doesn't.

So maybe we introduce this as a second mode simply, you lose sourcemaps and window.onerror support but gain interactive execution.

@juliangruber
Copy link
Owner

juliangruber commented Sep 28, 2016

Some javascript will even break by this execution technique, imagine for example those two snippets arriving in separate chunks

console.log(typeof foo)
var foo = 'bar'

The output is going to be undefined, whereas in full execution it would be "string", because of variable hoisting.

Therefore interactive mode is strictly to be seen as interactive / repl mode, it's not a way of improving performance for regular scripts.

It needs to work with copy / pasting regular scripts though too, as do the nodejs / chrome repl, so this needs to be coupled to IO. I'm thinking this should then be its own module, wrapping electron-stream.

@RangerMauve
Copy link

@juliangruber Just to confirm, in your example it wouldn't be "string". Variable hosting does not hoist the assignment. It would still be undefined until the line when the value gets assigned to the variable.

console.log(typeof foo);
var foo = 'bar'

Turns into

var foo
console.log(typeof foo)
foo = 'bar'

What it wouldn't work with is function hoisting since the value does get hoisted to the top in that case.

@juliangruber
Copy link
Owner

yeah you're right about that, thanks for the clarification. Anyway, the important part is that interactive mode can break javascript if not done correctly.

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

4 participants