B4J Library jScriptEngine

===================================
The Nashorn scriptengine is being removed in java 11
===================================
(but it hasn't gone yet. See post #26 of this thread)


Important:
the scriptengine has nothing to do with browsers nashorn cannot be run in a web page, it is for server side applications.

Here is a small library that allows use of javascript from B4J.
Some functions are easier to perform in js as you have basically all the java functions at your disposal to use.
The B4J project is overly commented to show how to use the lib.
It has example of converting a uniicode string to a string B4J understands.
It implements a crude split function.
The js script can be in-line or read from an external file.

The jNashorn (o-r-n) is the updated library using the preferred script engine for javaFX 8

Almost identical to jScriptEngine in use, but the engine itself very powerful.
Read up on Nashorn to see the full extent of the engine. Requires Java 8 to run.

UPDATE
Found a useful bit of code to allow access to b4j variables and routines from within the js script
(tested on Java 8, with jScriptEngine and jNashorn libraries)
B4X:
   ...
   Dim js As jScriptEngine
   Dim ss As String = Me
   ss = ss.SubString(ss.IndexOf(" ")+1) ' build class name b4j.example.main is default
   js.evalstring($"var b4j = Java.type(""$ & ss & $"");"$)
   js.evalString($"b4j._test(" there");"$) '<<< runs b4j sub test
End Sub
Sub test(s As String)
   Log("hello" & s)
End Sub

=================================================================
UPDATE 28/10/15

Added 2 new files
nashorn invocable test ( the test file)
jInvokeNashorn (the library)

The first is a test file for the second , which is a new version of the nashorn library.

This version is quite different to the original jNashorn library
It does not support engineGet/Put , evalString or evalFile.

It simply has 2 entry points,
1, initInvocable()
2, Invoke()

initInvocable takes one parameter, it can be a string containing the script or the name of a script file (ending with .js) , if it is a script file it will look in the folder ./scripts for the file.

Invoke takes as many parameters as you like, but the first is always the function name in the script you wish to run.

The test file is well commented on how to use the new library.
You no longer need the get/put/eval in the original library as you can directly invoke functions in the script, which can return values or even call b4j subs directly with parameters.

Forgot to comment some lines in the test program.
the dim b4j as string
and
b4j = me
b4j = b4j.replace("class ","")
is needed to allow the script to access the b4j subs and variables directly.
 

Attachments

  • jScriptTest.zip
    1.6 KB · Views: 986
  • jScriptEngine.zip
    2.3 KB · Views: 1,638
  • jNashorn.zip
    2.3 KB · Views: 810
  • nashorn invocable test.zip
    1.4 KB · Views: 569
  • jInvokeNashorn.zip
    2.7 KB · Views: 654
  • reels example.zip
    5.1 KB · Views: 659
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Not sure I understand what you want to do, remember the jScriptEngine is for server side, it doesn't work inside a webview, for that you would use 'normal' javascript embedded in the page.
 

wl

Well-Known Member
Licensed User
Longtime User
Hi,

What kind of interaction is supported between B4J code and the Javascript engine ? Can I pass maps, custom objects, lists, ... between JS and B4Xcode ?

Tx !
 

Daestrum

Expert
Licensed User
Longtime User
==========================================================
Important information : Nashorn scriptengine does not exist in Java 11 it has been removed.
==========================================================
 

Daestrum

Expert
Licensed User
Longtime User
Note to the above note, Nashorn is still in java 11 but you get a warning
Warning: Nashorn engine is planned to be removed from a future JDK release
 
Top