Page 1 of 1

JSc - JavaScript in Purebasic

Posted: Wed Jan 24, 2018 11:54 am
by Kirito
As this is my first post here, let me introduce myself shortly:
I'm 24 years old and started using PureBasic as I was 12 (I guess :mrgreen:)
Came across a lot of other programming languages, as I made my
way to become a sofware engineer. All the other languages in my mind -
I still often use PureBasic, because it is ... well.. just beautiful and handy :)

Now back to topic 8)
My harddrive was a mess, that's why I decided to clean it up and found
some codes. Maybe this one is usefull for anyone out there :mrgreen:
I needed some code to communicate with JS, so I came across this
simple solution: On a hidden window, WebGadgets are created and run
my own 'Scripts'. I can put in parameters and get parameters out, all of them
in JSON-Format. The code itself injects return/update-functions, so it's simple
to use.

It should (hopefully) be clear what the code does, but let's take a look on this function:

Code: Select all

JSc_Script(Name$, Script$, ParamIn$, RunOnce.l=1, jq.b=0)
RunOnce:
01: Code runs once and is 'Ready'
00: Code runs unlimited
-1: Code restarts (untested)
-2: Code restarts, output becomes input (untested)

JQ:
00: Nothing
01: Inject JQuery

Download: http://www.mediafire.com/file/uzls8jpyn ... mpiler.zip (Includes + Example)

Here's an example-code:

Code: Select all

; Library for simple usage of JS-Scripts in PureBasic

  IncludeFile "_Init.pb"
  IncludeFile "_IDLE.pb"
  
;::::::::::::::::::::::::::::::::::::::::::::::::::::
  
  
  ;ScriptA with JQuery in it
  ScriptA$ = "var x=$('body');"
  ScriptA$ + "x.addClass('testClass');"
  ScriptA$ + "jsc_return(x.attr('class'));"
  
  ;ScriptB with simple use of arguments, Math and Strings
  ScriptB$ = "var x= args.name + 5*3*213*5.4+'_2secondscript_test2';"
  ScriptB$ + "jsc_return(x);"
  
  ;ScriptC with counter
  ScriptC$ = "window.setInterval('args.count++;" ;Every 0.300 secs new Output
  ScriptC$ + "jsc_return(args.count)',300);"
  
  ;ScriptD with Prompt and User-Data
  ScriptD$ = "jsc_return(prompt('Get from User','Default'));"
  
  ;Register Scripts
  JSc_Script("script_a", ScriptA$, "{'name':'test'}",1,1)
  JSc_Script("script_b", ScriptB$, "{'name':'test'}")
  JSc_Script("script_c", ScriptC$, "{'count':0}", 0)
  JSc_Script("script_d", ScriptD$, "{}")
  
  ;Run Scripts
  OpenConsole("JSC")

  While JSc_Script_Ready("script_a") <> 1 Or JSc_Script_Ready("script_b") <> 1  Or Val(JSc_Script_Out("script_c")) < 5
    JSc_IDLE()
    Delay(1)
    
    PrintN("a: "+JSc_Script_Out("script_a"))
    PrintN("b: "+JSc_Script_Out("script_b"))
    PrintN("c: "+JSc_Script_Out("script_c"))
    PrintN("d: "+JSc_Script_Out("script_d"))
    PrintN("======")
  Wend 

  
  ;Get Final-Results
  MessageRequester("Out", JSc_Script_Out("script_a")) ;Returns are in JSON_Format
  MessageRequester("Out", JSc_Script_Out("script_b"))
  MessageRequester("Out", JSc_Script_Out("script_c"))
  MessageRequester("Out", JSc_Script_Out("script_d"))
  
; =======================================
I just wanted to play around a little and in the end - I used another solution.
Maybe it helps someone to build there own way to use JS in PureBasic.
That said: I'm not going to update this code anymore, but if you want
feel free to develop it further :)

Have a nice day,
Kirito :)

Re: JSc - JavaScript in Purebasic

Posted: Tue Jan 30, 2018 11:45 am
by Kwai chang caine
It can be useful this style of program
Thanks for sharing 8)

Re: JSc - JavaScript in Purebasic

Posted: Tue Jan 30, 2018 1:03 pm
by Kirito
Kwai chang caine wrote:It can be useful this style of program
Thanks for sharing 8)
No problem :wink:

Re: JSc - JavaScript in Purebasic

Posted: Fri Jun 15, 2018 11:48 pm
by idle
thanks for sharing, looks great!