jsish
Using jsish
Not logged in

Invocation

The following describes a few of the ways to use jsish.

Interactive

Interactive mode is the easiest way to try out code snippets, eg:

  ./jsish
# var a = [1,2,3];
# for (var i in a) { puts(a[i]); }
1
2
3
...

Script

The script file to be executed is the first argument:

jsish prog.js arg1 arg2

Under unix, the first line of executable scripts can be #!:

#!/usr/bin/env jsish
for (var i in console.args)
   puts(console.args[i]);

The above allows jsish to be found in the path.

Inline

Javascript can also be evaluated from the command-line with -e, eg:
jsish -e 'var i = 0; while (i++<10) puts(i);'

Switches

To see the supported switches in jsish use -h

jsish -h
usage: jsi -h|--help|-v|--version|-d|--debug|-D|--debugui|-W|--websrv|-S|--safe|-Z|--zip|-H|--htmli|
	-J|-jsi|-C|--cssi|-i|--invoke CMD|-e|--eval SCRIPT|-t|--tracecall|-T|--typecheck OPT|-IOPT VAL|FILE arg arg ...

The available switches are:

Options Description
-h|--help Outputs jsish help, or module help
-v|--version Output jsish version information
-d|--debug Command-line debugger module (Sys)
-D|--debugui Jsi web-based gui debugger (Sys)
-W|--websrv Web server and start browser (Sys)
-S|--safe Run a safe interpreter, script or interactive (Sys)
-Z|--zip File zip utility (Sys)
-H|--htmli Html preprocessor (Sys)
-J|-jsi Javascript preprocessor (Sys)
-c|--cdata FILE Process a Cdata definition (Sys)
-C|--cssi Css preprocessor (Sys)
-i|--invoke CMD Invoke command Sys module stored in /zvfs
-e|--eval SCRIPT Evaluate a script string from command-line
-t|--tracecall Trace function calls and returned values
-T|--typecheck OPT Set function type-checking level
-IOPT VAL Set an interp option

Help for module commands can similarly be displayed, eg:

jsish -d -h
/zvfs/lib/JsiUtil.jsi:121: error: throw: 
A command-line Debugger for Jsi scripts.

Options/defaults:
	-debug	 	false		// Debugging output
	-echoCmd 	true		// Echo user cmds.
	-safe	 	false		// Debug program in a safe interp (untested)

ERROR

which is used as:

jsish -d -debug true tests/while.js

Options

Options are parameters handled by builtin commands. These get passed in an object:

var db = new Sqlite('/tmp/testsql.db',{maxStmts:1000, readonly:true});

Usually there is a conf() method providing access to options after creation:

Interp.conf(); // Dump all options
Interp.conf('strict'); // Get one option
Interp.conf({strict:false, maxDepth:99}); // Set one or more options.

Note some options may be readOnly. And an option that is initOnly may only be set at object creation time.

Internally, option parsing is implemented via C-Options.


Introspection

There are several levels of introspection built-in to Jsi. One is displayed when calling an invalid method:

# Array.xxx()
error: 'Array' sub-commands are: concat fill filter forEach indexOf join lastIndexOf map pop push reverse shift sizeOf slice some sort splice unshift.    (at or near "xxx")

Another, is querying with the Info command:

# Info.platform()
{ crc:7, hasThreads:true, intSize:4, isBigEndian:false, numberSize:8, os:"linux", platform:"unix", pointerSize:8, timeSize:8, wideSize:8 }

# Info.cmds()
[  "Array", "Boolean", "File", "FileIO", "Function", "Interp", "Info", "JSON", "Math", "Number",
   "Object", "RegExp", "Signal", "Sqlite", "String", "Sys", "Websocket", "Zvfs", "assert", "clearInterval",
   "console", "decodeURI", "encodeURI", "exit", "source",
   "isFinite", "isNaN", "load", "parseFloat", "parseInt", "puts", "quote",
   "setInterval", "setTimeout", "format" ]

# Info.cmds('Array.*')
[ "concat", "fill", "filter", "forEach", "indexOf", "join", "lastIndexOf", "map", "pop", "push",
  "reverse", "shift", "sizeOf", "slice", "some", "sort", "splice", "unshift" ]

# Info.named()
[ "Socket", "Channel", "MySql", "WebSocket", "Sqlite", "Interp" ]

var ii = new Interp();
# Info.named('Interp')
[ "#Interp_1" ]

and so on.


Example Applications

Jsi is distributed with several demonstration web applications:

These can all be run as standalone applications.


Shell

You can use jsish as an enhanced replacement for #!/usr/bin/env. This lets you run scripts from the command line with predefined arguments.

#!/usr/local/bin/jsish -T debug %s -trace true myinput1.txt
puts(console.args.join(' '));

(there must be a %s and at least one argument)

From geany you can now run the script with F9, and step through warnings and LogDebug messages.

Here is a bigger example: mytest2

For running non-jsi scripts, there is also a script env.jsi.

#!/usr/local/bin/env.jsi python -u %s

You can also try setting up .bashrc with autocompletion for Jsi using:

make setup.

Then (once you restart) typing TAB will perform autocompletion.

This can make running jsish from the command-line marginally nicer.

Although jsish -h also dumps out its options.


Editors

Geany

Geany is a convenient editor to use with Jsi. To enable Jsi file completion with Geany:

Geany can also navigate through Jsi's gcc style scripting errors:

Now hit F8 to run the script. Upon errors, you should be able to navigate to the highlighted lines, and see warnings in the bottom pane.

Alternatively, you can just create a Makefile to run jsish.

Finally, if using the logging feature in Jsi, you can also navigate through debug messages when using F9 or shell exec.

Caveat: one limitation of Geany is that a function with a return type will likely not show up in the symbols list.

Vim

Here is how to setup/use vim with Jsi:
:set filetype javascript
:set makeprg=jsish\ %
:copen

Then to run scripts just use:

:make

And much of what was said about navigation in Geany also applies to Vim.