-1

After reading Running V8 Javascript Engine Standalone decided to try installing V8 for the express purpose of running JavaScript at terminal.

Following guidance at Install depot_tools

$ sudo apt-get git
$ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
$ export PATH=`pwd`/depot_tools:"$PATH"

Though have not yet built [V8][2]. Instead, first tried the suggestion at linked Question of using nodejs. Note, have limited experience with nodejs other than attempting to use nodejs to resolve How to test if jQuery 3.0 beta is Promises/A+ compatible in browser?

$ sudo apt-get install nodejs
> x = 10*5
50
> x
50
> let {id} = {id:123}
... 
... id
... 
> id
ReferenceError: id is not defined
    at repl:1:1
    at REPLServer.defaultEval (repl.js:252:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:417:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:210:10)
    at REPLServer.Interface._line (readline.js:549:8)
    at REPLServer.Interface._ttyWrite (readline.js:826:14)
> {id} = {id:123}
ReferenceError: Invalid left-hand side in assignment
    at Object.exports.createScript (vm.js:24:10)
    at REPLServer.defaultEval (repl.js:225:25)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:417:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:210:10)
    at REPLServer.Interface._line (readline.js:549:8)
    at REPLServer.Interface._ttyWrite (readline.js:826:14)
> x
50
> var y = 456
undefined
> y
456
> var [a, b] = [1,2] 
... a
... 

> function test(a = 123) {return a}
... test(5)
... 
... 

Questions:

  • What is repl ?
  • Why is nodejs not recognizing destructuring assignment at repl at terminal?
  • What does ... mean at terminal when running nodejs?
  • How to run JavaScript commands at terminal with nodejs, including for example destructuring assignment; default parameters; defining and calling functions; other common usages, which do not result in ...?
  • How to correctly build V8 for purpose of running JavaScript at terminal?
Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177

1 Answers1

1

(1) REPL = "Read-Eval-Print Loop", the kind of interactive command-line you have in shells for various programming languages. See https://en.wikipedia.org/wiki/REPL for more details.

(2) ... means that the node shells thinks that your input is not yet complete. One special case where this can happen is if your input cannot be parsed without errors (e.g. if you're using unsupported language features). Here's an example where ... is useful:

> function test(a) {
... return a;
... }
undefined
> test(5)
5
> 

This also answers your question how to define/call/run stuff in general.

(3) In the "parse error" case, you can break out of ... with Ctrl+C.

(4) To build V8, follow the instructions at https://github.com/v8/v8/wiki/Building-with-GN. You'll get the d8 shell, which supports all the JavaScript features that V8 supports. Note, however, that it provides much less system integration (e.g. file manipulation) than node.js, because d8 is intended for running tests and other V8 development use cases; it doesn't try to be useful as a general-purpose shell.

jmrk
  • 34,271
  • 7
  • 59
  • 74