JerryScript Windows/Linux 32/64-bit

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
D.J.Peters
Posts: 7936
Joined: May 28, 2005 3:28

JerryScript Windows/Linux 32/64-bit

Postby D.J.Peters » Mar 29, 2018 18:10

Image
For advanced FreeBASIC users :-)

JerryScript a JavaScript engine for Internet of Things.
Full ECMAScript 5.1 standard compliance.
on github

Why JerryScript ?
I used JavaScript with FreeBASIC at my business more than once in the past.
For this job I compiled SpiderMonkey from Mozilla project for FreeBASIC.
But SpiderMonkey Version 1.8x was the last JavaScript engine with a C interface.
Today SpiderMonkey a part of Firefox has a pure C++ interface only.


latest verion from: Tue Feb 11, 2020 I prever the static version !
download static build: jerryscript-static.zip
download dynamic build: jerryscript.zip
download source code: jerryscript-src.zip

API examples: https://github.com/jerryscript-project/ ... EXAMPLE.md

Joshy
Last edited by D.J.Peters on Feb 10, 2020 23:21, edited 8 times in total.
srvaldez
Posts: 2215
Joined: Sep 25, 2005 21:54

Re: JerryScript Windows/Linux 32/64-bit

Postby srvaldez » Mar 29, 2018 23:14

hello D.J.Peters
what's the minimum version of windows that this would work on? (would it work on Windows 98)?
D.J.Peters
Posts: 7936
Joined: May 28, 2005 3:28

Re: JerryScript Windows/Linux 32/64-bit

Postby D.J.Peters » Mar 30, 2018 1:15

srvaldez wrote:(would it work on Windows 98)?
Why not ?
There isn't any Windows code it's POSIX and C lib.
I can test it only on XP so download and try it out.

But never and I mean NEVER !!!
go online with a running WIN 98 version.

Joshy
srvaldez
Posts: 2215
Joined: Sep 25, 2005 21:54

Re: JerryScript Windows/Linux 32/64-bit

Postby srvaldez » Mar 30, 2018 1:25

D.J.Peters wrote:But never and I mean NEVER !!!
go online with a running WIN 98 version.
Joshy

I am not sure what you meant by that, am guessing that it would not be safe?
in my case it would be run offline, no internet connection.
D.J.Peters
Posts: 7936
Joined: May 28, 2005 3:28

Re: JerryScript Windows/Linux 32/64-bit

Postby D.J.Peters » Mar 30, 2018 5:41

New version published the most important function "jerryx_handler_register_global" was missing in my first build :-(

By the way if you compile POSIX C99 stuff on Windows and Linux -std=C99 isn't the same as -std=gnu99

struct timezone tz;
if I compile with -std=C99 I get an error "size of timezone is unknow" !
I don't know why but If I use -std=gnu99 the error are gone.

How ever I added test02.bas and test03.bas

Joshy

file: test02.bas

Code: Select all

#include once "jerryscript.bi"

'
' main
'
jerry_init(JERRY_INIT_EMPTY)
jerryx_handler_register_global ("print",@jerryx_handler_print)
var script = "print('A String printed with C lib.');"
var code   = jerry_parse(script,len(script),false)
if (not jerry_value_has_error_flag(code)) then
  ' Execute the parsed source code in the Global scope
  var ret_value = jerry_run(code)
  jerry_release_value(ret_value)
end if
jerry_release_value(code)
jerry_cleanup()
sleep

file: test03.bas

Code: Select all

#include once "jerryscript.bi"

function MyNativeStringPrinter cdecl(function_obj_val as const jerry_value_t, _
                                     this_val         as const jerry_value_t, _
                                     args_p           as const jerry_value_t ptr, _
                                     args_cnt         as const jerry_length_t) as jerry_value_t
  if ((args_cnt=1) andalso jerry_value_is_string(args_p[0]))then
    var nChars = jerry_get_string_size(args_p[0])
    dim as zstring ptr buffer = allocate(nChars + 1)
    jerry_string_to_char_buffer(args_p[0], buffer, nChars)
    buffer[nChars] = 0
    print *buffer
    deallocate(buffer)
  end if
  return jerry_create_boolean(true)
end function

'
' main
'
jerry_init(JERRY_INIT_EMPTY)
jerryx_handler_register_global ("printString",@MyNativeStringPrinter)
var script = "printString('A string printed with FreeBASIC');"
var code   = jerry_parse(script,len(script),false)
if (not jerry_value_has_error_flag(code)) then
  ' Execute the parsed source code in the Global scope
  var ret_value = jerry_run(code)
  jerry_release_value(ret_value)
end if
jerry_release_value(code)
jerry_cleanup()
sleep
D.J.Peters
Posts: 7936
Joined: May 28, 2005 3:28

Re: JerryScript Windows/Linux 32/64-bit

Postby D.J.Peters » Mar 30, 2018 13:30

I build a static version of JerryScript also see first post.
No more run-time dependencies :-)

Joshy
srvaldez
Posts: 2215
Joined: Sep 25, 2005 21:54

Re: JerryScript Windows/Linux 32/64-bit

Postby srvaldez » Mar 30, 2018 19:16

thank you D.J.Peters :-)
Iczer
Posts: 54
Joined: Jul 04, 2017 18:09

Re: JerryScript Windows/Linux 32/64-bit

Postby Iczer » Apr 01, 2018 16:19

D.J.Peters
Do you think FB64@JerryScript can compile WanaKana from https://github.com/WaniKani/WanaKana - so I can use use it functions (.toRomaji etc...) in my windows dll?
D.J.Peters
Posts: 7936
Joined: May 28, 2005 3:28

Re: JerryScript Windows/Linux 32/64-bit

Postby D.J.Peters » Apr 01, 2018 17:31

@Iczer JerryScript is a full ECMAScript 5.1 JavaScript compiler and Virtual Machine.
It comes with some predefined objects like Date, Math, JSON ...
If the lib you mean used only this standard objects you can run it with JerryScript.
But often JS libraries used objects from WEB Browser DOM, Window, Canvas ...
you have to implement this missing objects self.

You must be an advanced programmer to do this kind of heavy stuff.

Joshy
Iczer
Posts: 54
Joined: Jul 04, 2017 18:09

Re: JerryScript Windows/Linux 32/64-bit

Postby Iczer » Apr 02, 2018 7:44

Thanks!
D.J.Peters
Posts: 7936
Joined: May 28, 2005 3:28

Re: JerryScript Windows/Linux 32/64-bit

Postby D.J.Peters » Feb 10, 2020 23:17

After two years I found a bug in the jerryscript source code float vs double !

How ever I rebuld all static and dynamic libraries for Windows Linux 32/64-bit and added a more usefull example.

Joshy

file: "test04.bas"

Code: Select all

'#define JERRY_USE_FLOAT
#include once "jerryscript.bi"


' Add param to 'this.x'

function add_handler cdecl (func_value as const jerry_value_t , _ ' function object
                            this_val   as const jerry_value_t , _ 'this arg
                            args_p     as const jerry_value_t ptr , _ 'function arguments
                            args_cnt   as const jerry_length_t ) as jerry_value_t ' number of function arguments

  ' The the 'this_val' is the 'MyObject' from the JS code below
  ' Note: that the argument count check is ignored for the example's case

  ' Get 'this.x'
  var prop_name = jerry_create_string ("x")
  var x_val = jerry_get_property (this_val, prop_name)

  if (not jerry_value_has_error_flag (x_val)) then
 
    ' Convert Jerry API values to floating point number
    var x = jerry_get_number_value (x_val)
    var d = jerry_get_number_value (args_p[0])

    ' Add the parameter to 'x'
    var res_val = jerry_create_number (x + d)
     ' Set the new value of 'this.x'
    jerry_release_value (jerry_set_property (this_val, prop_name, res_val))
    jerry_release_value (res_val)
  end if

  jerry_release_value (x_val)
  jerry_release_value (prop_name)

  return jerry_create_undefined ()
end function

'
' main
'
' Initialize engine
jerry_init (JERRY_INIT_EMPTY)

' Register 'print' function from the C extensions
jerryx_handler_register_global ("print", @jerryx_handler_print)

' Create a JS object
var my_js_object = _
"MyObject = {" & _
"  x : 12, y : 'Value of x is ', " & _
"foo: function () { return this.y + this.x; }" & _
"};"

' Evaluate script
var my_js_obj_val = jerry_eval (my_js_object, len(my_js_object), false)

' Create a JS function object and wrap into a jerry value
var add_func_obj = jerry_create_external_function (@add_handler)

' Set the native function as a property of previously created MyObject
var prop_name = jerry_create_string ("add2x")
jerry_release_value (jerry_set_property (my_js_obj_val, prop_name, add_func_obj))
jerry_release_value (add_func_obj)
jerry_release_value (prop_name)

' Free JavaScript value, returned by eval (my_js_object)/
jerry_release_value (my_js_obj_val)

var script = " var str = MyObject.foo();" _
& "  print(str); " _
& "  MyObject.add2x(30); " _
& "  print (MyObject.foo());"

' Evaluate script
dim as jerry_value_t eval_ret = jerry_eval (script, len(script), false)

' Free JavaScript value, returned by eval
jerry_release_value (eval_ret)

' Cleanup engine
jerry_cleanup ()
sleep

Return to “Libraries”

Who is online

Users browsing this forum: MSN [Bot] and 2 guests