JSc - JavaScript in Purebasic

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
Kirito
New User
New User
Posts: 5
Joined: Wed Jan 24, 2018 11:38 am

JSc - JavaScript in Purebasic

Post 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 :)
~ Lately, there are days I can’t even remember the other world.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: JSc - JavaScript in Purebasic

Post by Kwai chang caine »

It can be useful this style of program
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
Kirito
New User
New User
Posts: 5
Joined: Wed Jan 24, 2018 11:38 am

Re: JSc - JavaScript in Purebasic

Post by Kirito »

Kwai chang caine wrote:It can be useful this style of program
Thanks for sharing 8)
No problem :wink:
~ Lately, there are days I can’t even remember the other world.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: JSc - JavaScript in Purebasic

Post by idle »

thanks for sharing, looks great!
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply