Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Support

Pages: [1] 2 3 ... 12
1
Source / LCASE / UCASE Bug
« on: October 01, 2022, 10:37:32 AM »
AIR,

As mentioned in the e-mail I sent you, Peter said that these functions need a new mortal variable and the passed variable should be only used for reference only.

Code: Script BASIC
  1. a = "Account"
  2. b = LCASE(a)
  3. PRINT a, "\n"
  4. PRINT b, "\n"
  5.  


C:\sbqbo>sbc t_lcase.sb
account
account

C:\sbqbo>


2
What's New / ScriptBasic Windows 32 bit
« on: September 18, 2022, 04:58:36 PM »
I'm putting together another Windows release which I'll post soon.

What's New
  • Recompile with TDM-GCC-32 10.3.0 release.
  • Print.c change to use %g for doubles rather than %le.
  • Updated JSON extension module by AIR.
  • New webext extension that features a JSON to SB arrays and back again functions..
  • Updated ODBC extension allowing column length of 4096 bytes and schema extension to generate SQL CREATE statements or return details about a column.
I'm not going to be including help or examples in the install. Those resources are available from the ScriptBasic forum.

Stay tuned ...

3
What's New / SBIDE
« on: May 28, 2021, 08:17:33 PM »
I'm adding the core extension module FUNCTION/SUB syntax helpers to SBIDE's syntax tip help. After entering a keyword or extension function name and a ( the syntax tool tip will display. As you enter arguments the tool tip will move to the next argument. Once I completed the SBIDE enhancements I will post it here. I will include the updated SBIDE in the next Inno install build.

Hot Keys

ctrl-f - find/replace
ctrl-g - goto line
ctrl-z - undo
ctrl-y - redo

F2     - set breakpoint (runtime only)
F5     - go
F7     - single step
F8     - step over
F9     - step out


4
What's New / ScriptBasic Core Windows 32 bit Install
« on: May 25, 2021, 03:07:09 PM »
The attached ScriptBasic Windows install is a core distribution with the essential extension modules included. A User Guide is included as a Windows .CHM help file. The following posts to this thread are examples that are also included with the install program.



Core Extension Modules
  • COM/OLE Automation
  • cURL Library
  • ODBC
  • SQLite
  • Dynamic FFI
  • Asynchronous / Synchronous Threading

ScriptBasic User Guide

ScriptBasic SandBox Repository

ScriptBasic Download Attached

5
Documentation / ScriptBasic Support
« on: January 02, 2021, 03:09:09 AM »


The ScriptBasic (MIT Common License) open source project is maintained by John Spikowski. (Peter Verhas - original author) I'm a senior developer offering affordable programming support for ScriptBasic, Sage 100 and Quickbooks accounting packages.


EMAIL: support@scriptbasic.org
LINKEDIN: John Spikowski
PHONE/TEXT: 360-941-0452
LOCATION: Anacortes, WA USA


6
What's New / ScriptBasic - Raspberry Pi
« on: March 30, 2019, 02:25:38 PM »
I'm pleased  to announce a release of ScriptBasic for the Raspberry Pi. You can do a traditional install with a .deb file or as an AppImage

This version of the binaries do NOT work on the Raspberry Pi Zero.
(Requires ARM v7 or greater)

DEB Install:

Code: Bash
  1. sudo apt install ./scriptbasic-2.1-linux-armhf.deb

DEB Uninstall:

Code: Bash
  1. sudo apt remove scriptbasic-2.1-linux-armhf

To run a script use scriba <script nane> when using the DEB Install.

To use the AppImage version ScriptBasic, unzip the file in the /usr/local/bin (or a directory in the system path) and use sb <script_name> to run your scripts. To remove ScriptBasic, remove the sb file.

I have added scriptbasic-rpi-zero.deb as an attachment. This version will run on ALL versions of the Raspberry Pi where the scriptbasic-2.1-linux-armhf.deb and AppImage versiosn will only run on the ARM v7 versions of the boards. The same instructions apply just change the name of the .deb file.


ScriptBasic User and Developer Guides

Most of the ScriptBasic development activity is happening on the All BASIC Forum.

Language Features
  • Tradional BASIC syntax
  • Variant style variables (no definition or declaration  needed)
  • Indexed, associative arrays (or combo of both) no practical limits
  • Muilt-threaded - asynchronous / synchronous execution
  • Unlimited seamless expansion via the extension API
  • Embeddable API
  • Footprint less than 800 KB
  • Runs on everything with a single source tree (written in ANSI C)
  • Application proxy web server that runs ScriptBasic code
  • Cascading pre-processor support
  • Debugger with single step execution, break points and more

Extension Modules
  • libcurl + SSL
  • MySQL
  • SQLite
  • ODBC
  • json
  • XML
  • regex
  • CGI
  • zlib
  • SBT - thread support
  • IUP - cross platform portable GUI
  • SDL_gfx graphics primatives with alpha channel support
  • BBC BASIC graphic library
  • more ..

Free - Open Source - MIT License

Raspberry Pi Downloads Attached

7
What's New / Script BASIC Wiki
« on: August 14, 2018, 04:33:53 PM »
I have updated the Script BASIC Wiki to the latest version of Mediawiki. (what runs Wikipedia)

Direct Link
https://scriptbasic.org/wiki

I plan to document the Script BASIC COM extension module in the updated wiki. The wiki has been a resource I have neglected for some time.  :(


8
Download / Script BASIC Windows 32 bit Inno Install
« on: July 14, 2018, 12:19:29 AM »
I have attached a current Script BASIC Windows 32 bit Inno install. Give a try and let me know if you have any issues.


9
General Discussions / Script BASIC Arrays
« on: November 02, 2017, 02:58:53 PM »
One of my most favorite features of Script BASIC is its array (matrix) functionality. You don't have to define or dimension anything and there are no practical limits on indices. (associative, indexed or a combination of both)

Code: Script BASIC
  1. ' Test  Associative Array by Index
  2.  
  3. obj{"Name"}{"Value"} = undef
  4. obj{"Name"}{"Product"} = "ALL"
  5. obj{"Name"}{"Required"} = TRUE
  6. obj{"ID"}{"Value"} = undef
  7. obj{"ID"}{"Product"} = "SOME"
  8. obj{"ID"}{"Required"} = FALSE
  9.  
  10. this{"Extension"}{"Value"} = "Default"
  11. this{"Extension"}{"Product"} = "FEW"
  12. this{"Extension"}{"Required"} = "MAYBE"
  13.  
  14. obj{"Version"} = this{"Extension"}
  15.  
  16. FOR o = 0 TO UBOUND(obj) STEP 2
  17.   PRINT obj[o],"\n"
  18.   FOR p = 0 TO UBOUND(obj[o + 1]) STEP 2
  19.     PRINT obj[o + 1, p]," - ",obj[o + 1, p + 1],"\n"
  20.   NEXT
  21. NEXT
  22.  
  23. PRINT "-------------------------------","\n"
  24.  
  25. PRINT obj[0],"\n"
  26. PRINT obj[1,0]," - ",obj[1,1],"\n"
  27. PRINT obj[1,2]," - ",obj[1,3],"\n"
  28. PRINT obj[1,4]," - ",obj[1,5],"\n"
  29. PRINT obj[2],"\n"
  30. PRINT obj[3,0]," - ",obj[3,1],"\n"
  31. PRINT obj[3,2]," - ",obj[3,3],"\n"
  32. PRINT obj[3,4]," - ",obj[3,5],"\n"
  33. PRINT obj[4],"\n"
  34. PRINT obj[5,0]," - ",obj[5,1],"\n"
  35. PRINT obj[5,2]," - ",obj[5,3],"\n"
  36. PRINT obj[5,4]," - ",obj[5,5],"\n"
  37.  
  38.  
  39. PRINT "-------------------------------","\n"
  40.  
  41. PRINT obj[2],"\n"
  42. FOR z = 0 TO UBOUND(obj{"ID"}) STEP 2
  43.   PRINT obj{"ID"}[z]," - ",obj{"ID"}[z + 1],"\n"
  44. NEXT
  45.  


Name
Value - undef
Product - ALL
Required - -1
ID
Value - undef
Product - SOME
Required - 0
Version
Value - Default
Product - FEW
Required - MAYBE
-------------------------------
Name
Value - undef
Product - ALL
Required - -1
ID
Value - undef
Product - SOME
Required - 0
Version
Value - Default
Product - FEW
Required - MAYBE
-------------------------------
ID
Value - undef
Product - SOME
Required - 0


10
Extension Modules / Script BASIC JavaScript Extension Module (Linux)
« on: October 18, 2017, 10:25:25 PM »
The Script BASIC JS extension module for Linux is based on the Cesanta's V7 JavaScript Engine. It claims to be the world's smallest footprint JavaScript 5.1 compatible embeddable engine. There are no other dependencies required.

JavaScript Programmers Guide

V7 JavaScript Documentation

Features:
  • Dynamically create JavaScript code in Script BASIC and execute the script.,
  • Call JavaScript functions and access variables from Script BASIC
  • Create / change JavaScript object properties and methods from Script BASIC
  • and much more ...

Note: The \ character is used by Script BASIC as a string escape character and must be inserted in the JavaScript code to make the \ just text. Loading JavaScript code from a file doesn't have the escape character issue and can be run verbatim.

js.inc
Code: Script BASIC
  1. MODULE JS
  2.  
  3. ' CORE
  4. DECLARE SUB      js_create                  ALIAS "js_create"                 LIB "js"
  5. DECLARE SUB      js_destroy                 ALIAS "js_destroy"                LIB "js"
  6. DECLARE SUB      js_get_global              ALIAS "js_get_global"             LIB "js"
  7. DECLARE SUB      js_get_this                ALIAS "js_get_this"               LIB "js"
  8. DECLARE SUB      js_get_arguments           ALIAS "js_get_arguments"          LIB "js"
  9. DECLARE SUB      js_arg                     ALIAS "js_arg"                    LIB "js"
  10. DECLARE SUB      js_argc                    ALIAS "js_argc"                   LIB "js"
  11. DECLARE SUB      js_own                     ALIAS "js_own"                    LIB "js"
  12. DECLARE SUB      js_disown                  ALIAS "js_disown"                 LIB "js"
  13. DECLARE SUB      js_set_gc_enabled          ALIAS "js_set_gc_enabled"         LIB "js"
  14. DECLARE SUB      js_interrupt               ALIAS "js_interrupt"              LIB "js"
  15. DECLARE SUB      js_get_parser_error        ALIAS "js_get_parser_error"       LIB "js"
  16.  
  17. ' PRIMITIVES
  18. DECLARE SUB      js_mk_number               ALIAS "js_mk_number"              LIB "js"
  19. DECLARE SUB      js_get_double              ALIAS "js_get_double"             LIB "js"
  20. DECLARE SUB      js_get_int                 ALIAS "js_get_int"                LIB "js"
  21. DECLARE SUB      js_is_number               ALIAS "js_is_number"              LIB "js"
  22. DECLARE SUB      js_mk_boolean              ALIAS "js_mk_boolean"             LIB "js"
  23. DECLARE SUB      js_get_bool                ALIAS "js_get_bool"               LIB "js"
  24. DECLARE SUB      js_is_boolean              ALIAS "js_is_boolean"             LIB "js"
  25. DECLARE SUB      js_mk_null                 ALIAS "js_mk_null"                LIB "js"
  26. DECLARE SUB      js_is_null                 ALIAS "js_is_null"                LIB "js"
  27. DECLARE SUB      js_mk_undefined            ALIAS "js_mk_undefined"           LIB "js"
  28. DECLARE SUB      js_is_undefined            ALIAS "js_is_undefined"           LIB "js"
  29. DECLARE SUB      js_mk_foreign              ALIAS "js_mk_foreign"             LIB "js"
  30. DECLARE SUB      js_get_ptr                 ALIAS "js_get_ptr"                LIB "js"
  31. DECLARE SUB      js_is_foreign              ALIAS "js_is_foreign"             LIB "js"
  32.  
  33. ' STRINGS
  34. DECLARE SUB      js_mk_string               ALIAS "js_mk_string"              LIB "js"
  35. DECLARE SUB      js_is_string               ALIAS "js_is_string"              LIB "js"
  36. DECLARE SUB      js_get_string              ALIAS "js_get_string"             LIB "js"
  37. DECLARE SUB      js_get_cstring             ALIAS "js_get_cstring"            LIB "js"
  38.  
  39. ' OBJECTS
  40. DECLARE SUB      js_mk_object               ALIAS "js_mk_object"              LIB "js"
  41. DECLARE SUB      js_is_object               ALIAS "js_is_object"              LIB "js"
  42. DECLARE SUB      js_get_proto               ALIAS "js_get_proto"              LIB "js"
  43. DECLARE SUB      js_set_proto               ALIAS "js_set_proto"              LIB "js"
  44. DECLARE SUB      js_get                     ALIAS "js_get"                    LIB "js"
  45. DECLARE SUB      js_def                     ALIAS "js_def"                    LIB "js"
  46. DECLARE SUB      js_set                     ALIAS "js_set"                    LIB "js"
  47. DECLARE SUB      js_del                     ALIAS "js_del"                    LIB "js"
  48. DECLARE SUB      js_init_prop_iter_ctx      ALIAS "js_init_prop_iter_ctx"     LIB "js"
  49. DECLARE SUB      js_next_prop               ALIAS "js_next_prop"              LIB "js"
  50. DECLARE SUB      js_destruct_prop_iter_ctx  ALIAS "js_destruct_prop_iter_ctx" LIB "js"
  51. DECLARE SUB      js_is_instanceof           ALIAS "js_is_instanceof"          LIB "js"
  52. DECLARE SUB      js_is_instanceof_v         ALIAS "js_is_instanceof_v"        LIB "js"
  53.  
  54. ' ARRAYS
  55. DECLARE SUB      js_mk_array                ALIAS "js_mk_array"               LIB "js"
  56. DECLARE SUB      js_is_array                ALIAS "js_is_array"               LIB "js"
  57. DECLARE SUB      js_array_length            ALIAS "js_array_length"           LIB "js"
  58. DECLARE SUB      js_array_push              ALIAS "js_array_push"             LIB "js"
  59. DECLARE SUB      js_array_get               ALIAS "js_array_get"              LIB "js"
  60. DECLARE SUB      js_array_set               ALIAS "js_array_set"              LIB "js"
  61. DECLARE SUB      js_array_del               ALIAS "js_array_del"              LIB "js"
  62.  
  63. ' EXECUTION
  64. DECLARE SUB      js_exec                    ALIAS "js_exec"                   LIB "js"
  65. DECLARE SUB      js_exec_file               ALIAS "js_exec_file"              LIB "js"
  66. DECLARE SUB      js_apply                   ALIAS "js_apply"                  LIB "js"
  67. DECLARE SUB      js_parse_json              ALIAS "js_parse_json"             LIB "js"
  68. DECLARE SUB      js_parse_json_file         ALIAS "js_parse_json_file"        LIB "js"
  69.  
  70. 'REGEX
  71. DECLARE SUB      js_mk_regexp               ALIAS "js_mk_regexp"              LIB "js"
  72. DECLARE SUB      js_is_regexp               ALIAS "js_is_regexp"              LIB "js"
  73.  
  74. ' UTILITY
  75. DECLARE SUB      js_stringify               ALIAS "js_stringify"              LIB "js"
  76. DECLARE SUB      js_println                 ALIAS "js_println"                LIB "js"
  77.  
  78. DECLARE SUB      SB_shifts                  ALIAS "SB_shifts"                 LIB "js"
  79. DECLARE COMMAND  js_iif                     ALIAS "js_iif"                    LIB "js"
  80.  
  81.  
  82. ' JS Global Module Variables
  83. OBJ = 0
  84. SYS = 0
  85.  
  86. ' Stringify Modes
  87. DEFAULT = 0
  88. JSON    = 1
  89. DEBUG   = 2
  90.  
  91. ' Property Attribute Support
  92.  
  93. CONST V7_PROPERTY_NON_WRITABLE              = 1
  94. CONST V7_PROPERTY_NON_ENUMERABLE            = 2
  95. CONST V7_PROPERTY_NON_CONFIGURABLE          = 4
  96. CONST V7_PROPERTY_GETTER                    = 8
  97. CONST V7_PROPERTY_SETTER                    = 16
  98. CONST _V7_PROPERTY_HIDDEN                   = 32
  99. CONST _V7_PROPERTY_OFF_HEAP                 = 64
  100. CONST _V7_PROPERTY_USER_DATA_AND_DESTRUCTOR = 128
  101. CONST _V7_DESC_PRESERVE_VALUE               = 256
  102. CONST _V7_DESC_MASK                         = &HFFFF
  103.  
  104. CONST PROPERTY_DEFAULT = 0
  105.  
  106. ' TRUE or FALSE. Whether the property's value can be set.
  107. FUNCTION WRITABLE(v)
  108.   IF v THEN
  109.     WRITABLE = PROPERTY_DEFAULT
  110.   ELSE
  111.     WRITABLE = V7_PROPERTY_NON_WRITABLE
  112.   END IF
  113. END FUNCTION
  114.  
  115. ' TRUE or FALSE. Whether the property shows in some loop constructs.
  116. FUNCTION ENUMERABLE(v)
  117.   IF v THEN
  118.     ENUMERABLE = PROPERTY_DEFAULT
  119.   ELSE
  120.     ENUMERABLE = V7_PROPERTY_NON_ENUMERABLE
  121.   END IF
  122. END FUNCTION
  123.  
  124. ' TRUE or FALSE. Whether the property can be deleted and whether its attributes can be changed.
  125. FUNCTION CONFIGURABLE(v)
  126.   IF v THEN
  127.     CONFIGURABLE = PROPERTY_DEFAULT
  128.   ELSE
  129.     CONFIGURABLE = V7_PROPERTY_NON_CONFIGURABLE
  130.   END IF
  131. END FUNCTION
  132.  
  133. ' TRUE or FALSE. When a property is accessed the value is generated by calling a function implicitly.
  134. FUNCTION GETTER(v)
  135.   IF v THEN
  136.     GETTER = V7_PROPERTY_GETTER
  137.   ELSE
  138.     GETTER = FALSE
  139.   END IF
  140. END FUNCTION
  141.  
  142. ' TRUE or FALSE. When a property is set it will implicitly call a function and pass a
  143. ' value as argument, and the return value of the function is set to the property.
  144. FUNCTION SETTER(v)
  145.   IF v THEN
  146.     SETTER = V7_PROPERTY_SETTER
  147.   ELSE
  148.     SETTER = FALSE
  149.   END IF
  150. END FUNCTION
  151.  
  152. FUNCTION PRESERVE_VALUE
  153.     PRESERVE_VALUE = _V7_DESC_PRESERVE_VALUE
  154. END FUNCTION
  155.  
  156. FUNCTION HIDDEN(v)
  157.   IF v THEN
  158.     HIDDEN = V7_PROPERTY_HIDDEN
  159.   ELSE
  160.     HIDDEN = FALSE
  161.   END IF
  162. END FUNCTION
  163.  
  164. FUNCTION OFF_HEAP(v)
  165.   IF v THEN
  166.     OFF_HEAP = _V7_PROPERTY_OFF_HEAP
  167.   ELSE
  168.     OFF_HEAP = FALSE
  169.   END IF
  170. END FUNCTION
  171.  
  172.  
  173. ' JS API Function Wrappers
  174.  
  175. ' Create V7 instance
  176. FUNCTION CREATE
  177.   OBJ = js_create()
  178.   SYS = js_get_global(OBJ)
  179.   CREATE = OBJ
  180. END FUNCTION
  181.  
  182. ' Destroy V7 instance
  183. SUB DESTROY
  184.   js_destroy(OBJ)
  185.   UNDEF OBJ
  186. END SUB
  187.  
  188. ' Return root level (`global`) object of the given V7 instance
  189. FUNCTION GET_GLOBAL
  190.   GET_GLOBAL = js_get_global(OBJ)
  191. END FUNCTION
  192.  
  193. ' Return current `this` object
  194. FUNCTION GET_THIS
  195.   GET_THIS = js_get_this(OBJ)
  196. END FUNCTION
  197.  
  198. ' Return current `arguments` array
  199. FUNCTION GET_ARGUMENTS
  200.   GET_ARGUMENTS = js_get_arguments(OBJ)
  201. END FUNCTION
  202.  
  203. ' Return i-th argument
  204. FUNCTION ARG(i)
  205.   ARG = js_arg(OBJ, i)
  206. END FUNCTION
  207.  
  208. ' Return the length (`count`) of `arguments`
  209. FUNCTION ARGC
  210.   ARGC = js_argc(OBJ)
  211. END FUNCTION
  212.  
  213. ' Tells the GC about a JS value variable/field owned by `C` code.
  214. SUB OWN(v)
  215.   js_own(OBJ, v)
  216. END SUB
  217.  
  218. ' User code should also explicitly disown the variables with v7_disown
  219. ' once it goes out of scope or the structure containing the v7_val_t field is freed.
  220. ' Returns 1 if value is found, 0 otherwise
  221. FUNCTION DISOWN(v)
  222.   DISOWN = js_disown(OBJ, v)
  223. END FUNCTION
  224.  
  225. ' Enable or disable GC
  226. SUB SET_GC_ENABLED(enabled)
  227.   js_set_gc_enabled(OBJ, enabled)
  228. END SUB
  229.  
  230. ' It sets a flag that will cause the interpreter to throw an Interrupted Error
  231. SUB INTERRUPT
  232.   js_interrupt(OBJ)
  233. END SUB
  234.  
  235. ' Returns last parser error message
  236. FUNCTION GET_ERROR
  237.   GET_ERROR = js_get_parser_error(OBJ)
  238. END FUNCTION
  239.  
  240. ' Make numeric primitive value
  241. FUNCTION MK_NUMBER(num)
  242.   MK_NUMBER = js_mk_number(OBJ, num)
  243. END FUNCTION
  244.  
  245. ' Returns number value stored in `v7_val_t` as `double`
  246. FUNCTION GET_DOUBLE(v)
  247.   GET_DOUBLE = js_get_double(OBJ, v)
  248. END FUNCTION
  249.  
  250. ' Returns number value stored in `v7_val_t` as `int`. If the number
  251. ' value is not an integer, the fraction part will be discarded.
  252. FUNCTION GET_INT(v)
  253.   GET_INT = js_get_int(OBJ, v)
  254. END FUNCTION
  255.  
  256. ' Returns true if given value is a primitive number value
  257. FUNCTION IS_NUMBER(v)
  258.   IS_NUMBER = js_is_number(v)
  259. END FUNCTION
  260.  
  261. ' Make boolean primitive value (either `true` or `false`)
  262. FUNCTION MK_BOOLEAN(is_true)
  263.   MK_BOOLEAN = js_mk_boolean(OBJ, is_true)
  264. END FUNCTION
  265.  
  266. ' Returns boolean stored in `v7_val_t`: 0 for `false` or
  267. ' non-boolean, non-0 for `true`
  268. FUNCTION GET_BOOL(v)
  269.   GET_BOOL = js_get_bool(OBJ, v)
  270. END FUNCTION
  271.  
  272. ' Returns `true` if given value is a primitive boolean value
  273. FUNCTION IS_BOOLEAN(v)
  274.   IS_BOOLEAN = js_is_boolean(v)
  275. END FUNCTION
  276.  
  277. ' Make `null` primitive value
  278. FUNCTION MK_NULL
  279.   MK_NULL = js_mk_null()
  280. END FUNCTION
  281.  
  282. ' Returns true if given value is a primitive `null` value
  283. FUNCTION IS_NULL(v)
  284.   IS_NULL = js_is_null(v)
  285. END FUNCTION
  286.  
  287. ' Make `undefined` primitive value
  288. FUNCTION MK_UNDEFINED
  289.   MK_UNDEFINED = js_mk_undefined()
  290. END FUNCTION
  291.  
  292. ' Returns true if given value is a primitive `undefined` value
  293. FUNCTION IS_UNDEFINED(v)
  294.   IS_UNDEFINED = js_is_undefined(v)
  295. END FUNCTION
  296.  
  297. ' Make JavaScript value that holds C/C++ `void *` pointer
  298. FUNCTION MK_FOREIGN
  299.   MK_FOREIGN = js_mk_foreign(OBJ)
  300. END FUNCTION
  301.  
  302. ' Returns `void *` pointer stored in `v7_val_t`
  303. ' Returns NULL `undef` if the value is not a foreign pointer
  304. FUNCTION GET_PTR(v)
  305.   GET_PTR = js_get_ptr(OBJ, v)
  306. END FUNCTION
  307.  
  308. ' Returns true if given value holds `void *` pointer
  309. FUNCTION IS_FOREIGN(v)
  310.   IS_FOREIGN = js_is_foreign(v)
  311. END FUNCTION
  312.  
  313. ' Creates a string primitive value
  314. FUNCTION MK_STRING(strval)
  315.   MK_STRING = js_mk_string(OBJ, strval, LEN(strval), 1)
  316. END FUNCTION
  317.  
  318. ' Returns true if given value is a primitive string value
  319. FUNCTION IS_STRING(v)
  320.   IS_STRING = js_is_string(v)
  321. END FUNCTION
  322.  
  323. ' Returns a pointer to the string stored in `v7_val_t`
  324. FUNCTION GET_STRING(v)
  325.   GET_STRING = js_get_string(OBJ, v)
  326. END FUNCTION
  327.  
  328. ' Returns a pointer to the string stored in `v7_val_t`
  329. ' Returns NULL `undef` if the value is not a string or
  330. ' if the string is not compatible with a C string
  331. FUNCTION GET_CSTRING(v)
  332.   GET_CSTRING = js_get_cstring(OBJ, v)
  333. END FUNCTION
  334.  
  335. ' Make an empty object
  336. FUNCTION MK_OBJECT
  337.   MK_OBJECT = js_mk_object(OBJ)
  338. END FUNCTION
  339.  
  340. ' Returns true if the given value is an object or function
  341. FUNCTION IS_OBJECT(v)
  342.   IS_OBJECT = js_is_object(v)
  343. END FUNCTION
  344.  
  345. ' Get object's prototype.
  346. FUNCTION GET_PROTO(object)
  347.   GET_PROTO = js_get_proto(OBJ, object)
  348. END FUNCTION
  349.  
  350. ' Set object's prototype. Return old prototype or undefined on error
  351. FUNCTION SET_PROTO(object, proto)
  352.   SET_PROTO = js_set_proto(OBJ, object, proto)
  353. END FUNCTION
  354.  
  355. ' Lookup property `name` in object `obj`. If `obj` holds no such property,
  356. ' an `undefined` value is returned
  357. FUNCTION GETS(object, objname)
  358.   GETS = js_get(OBJ, object, objname, LEN(objname))
  359. END FUNCTION
  360.  
  361. ' Define object property, similar to JavaScript `Object.defineProperty()`
  362. FUNCTION DEF(object, objname, attr, value)
  363.   DEF = js_def(OBJ, object, objname, LEN(objname), attr, value)
  364. END FUNCTION
  365.  
  366. ' Set object property. Behaves just like JavaScript assignment
  367. FUNCTION SETS(object, objname, value)
  368.   SETS = js_set(OBJ, object, objname, LEN(objname), value)
  369. END FUNCTION
  370.  
  371. ' Delete own property `name` of the object `obj`
  372. ' Does not follow the prototype chain
  373. FUNCTION DEL(object, objname)
  374.   DEL = js_del(OBJ, object, objname, LEN(objname))
  375. END FUNCTION
  376.  
  377. ' Returns true if the object is an instance of a given constructor / class name
  378. FUNCTION IS_INSTANCEOF(object, classname)
  379.   IS_INSTANCEOF = js_is_instanceof(OBJ, object, classname)
  380. END FUNCTION
  381.  
  382. ' Returns true if the object is an instance of a given constructor object class
  383. FUNCTION IS_INSTANCEOF_V(object, objclass)
  384.   IS_INSTANCEOF_V = js_is_instanceof_v(OBJ, object, objclass)
  385. END FUNCTION
  386.  
  387. ' Custom multi-property `GET` function
  388. FUNCTION GET_PROPERTIES(object, proparray)
  389.   LOCAL objname, value, attr, propcnt
  390.   objname = ""
  391.   value = 0
  392.   attr = 0
  393.   propcnt = 1
  394.  ' UNDEF proparray
  395.  js_init_prop_iter_ctx(OBJ, object)
  396.   WHILE js_next_prop(OBJ, objname, value, attr) <> undef
  397.     proparray[propcnt, 0] = js_get_string(OBJ, objname)
  398.     proparray[propcnt, 1] = js_get_int(OBJ, value)
  399.     proparray[propcnt, 2] = attr
  400.     propcnt += 1
  401.   WEND
  402.   js_destruct_prop_iter_ctx(OBJ)
  403.   GET_PROPERTIES = propcnt - 1
  404. END FUNCTION
  405.  
  406. ' Make an empty array object
  407. FUNCTION MK_ARRAY
  408.   MK_ARRAY = js_mk_array(OBJ)
  409. END FUNCTION
  410.  
  411. ' Returns true if given value is an array object
  412. FUNCTION IS_ARRAY(object)
  413.   IS_ARRAY = js_is_array(OBJ, object)
  414. END FUNCTION
  415.  
  416. ' Returns length on an array. If `object` is not an array, 0 is returned
  417. FUNCTION ARRAY_LENGTH(object)
  418.   ARRAY_LENGTH = js_array_length(OBJ, object)
  419. END FUNCTION
  420.  
  421. ' Insert `value` in `object` at the end of the array
  422. FUNCTION ARRAY_PUSH(object, value)
  423.   ARRAY_PUSH = js_array_push(OBJ, object, value)
  424. END FUNCTION
  425.  
  426. '  Return array member at index `index`. If `index` is out of bounds, undefined is returned
  427. FUNCTION ARRAY_GET(object, index)
  428.   ARRAY_GET = js_array_get(OBJ, object, index)
  429. END FUNCTION
  430.  
  431. ' Insert value `v` into `arr` at 'index`
  432. FUNCTION ARRAY_SET(object, index, value)
  433.   ARRAY_SET = js_array_set(OBJ, object, index, value)
  434. END FUNCTION
  435.  
  436. ' Delete value in array `arr` at index `index`, if it exists
  437. SUB ARRAY_DEL(object, index)
  438.    js_array_del(OBJ, object, index)
  439. END SUB
  440.  
  441. ' Execute JavaScript `js_code`
  442. ' The result of evaluation is stored in the `return` variable
  443. ' The 'ok' argument will contain the function's execution success status flag
  444. FUNCTION EXEC(code, ok)
  445.   EXEC = js_exec(OBJ, code, ok)
  446. END FUNCTION
  447.  
  448. ' Same as `v7_exec()`, but loads source code from `path` file
  449. FUNCTION EXEC_FILE(codepath, ok)
  450.   EXEC_FILE = js_exec_file(OBJ, codepath, ok)
  451. END FUNCTION
  452.  
  453. ' Parse `json_code`
  454. ' The result of evaluation is stored in the `return` variable
  455. ' The 'ok' argument will contain the function's parse success status flag
  456. FUNCTION PARSE_JSON(json_code, ok)
  457.   PARSE_JSON = js_parse_json(OBJ, json_code, ok)
  458. END FUNCTION
  459.  
  460. ' Same as `v7_parse_json()`, but loads `json_code` from `path` file
  461. FUNCTION PARSE_JSON_FILE(json_code_file, ok)
  462.   PARSE_JSON_FILE = js_parse_json_file(OBJ, json_code_file, ok)
  463. END FUNCTION
  464.  
  465. ' Call function `func` with arguments `args`, using `object` as `this`
  466. ' `args` should be an array containing arguments or `undefined`
  467. FUNCTION APPLY(func, object, args)
  468.   APPLY = js_apply(OBJ, func, object, args)
  469. END FUNCTION
  470.  
  471. ' Make RegExp object. For example, `regex` is `(.+)`, `flags` is `gi`.
  472. FUNCTION MK_REGEXP(regex, flags, rcode)
  473.   MK_REGEXP = js_mk_regexp(OBJ, regex, LEN(regex), flags, LEN(flags), rcode)
  474. END FUNCTION
  475.  
  476. ' Returns true if given value is a JavaScript RegExp object
  477. FUNCTION IS_REGEXP(object)
  478.   IS_REGEXP = js_is_regexp(OBJ, object)
  479. END FUNCTION
  480.  
  481. ' Generate string representation of the JavaScript value
  482. FUNCTION STRINGIFY(object, convtype)
  483.   STRINGIFY = js_stringify(OBJ, object, convtype)
  484. END FUNCTION
  485.  
  486. ' Output a string representation of the value to stdout followed by a newline
  487. SUB PRINTIFY(object)
  488.   js_println(OBJ, object)
  489. END SUB
  490.  
  491.  
  492. END MODULE
  493.  


Hello JavaScript
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. JS::CREATE
  4. PRINT JS::GET_INT(JS::EXEC("1 + 1")),"\n"
  5. JS::DESTROY
  6.  


jrs@jrs-laptop:~/sb/examples/js$ time scriba js_hello2.sb
2

real   0m0.026s
user   0m0.016s
sys   0m0.008s
jrs@jrs-laptop:~/sb/examples/js$


Fibonacci
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. jscode = """
  4. function fibonacci(n) {
  5.  if (n <= 2) {
  6.    return 1;
  7.  } else {
  8.    return fibonacci(n - 1) + fibonacci(n - 2);
  9.  }
  10. }
  11.  
  12. print(fibonacci(24));
  13. """
  14.  
  15. JS::CREATE
  16. JS::EXEC(jscode)
  17. JS::DESTROY
  18.  


jrs@jrs-laptop:~/sb/examples/js$ scriba js_fibonacci.sb
46368
jrs@jrs-laptop:~/sb/examples/js$


Create object, property and attributes
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. JS::CREATE
  4. myobj = JS::MK_OBJECT()
  5. JS::DEF(myobj, "test", 0, JS::MK_NUMBER(64))
  6. JS::SETS(myobj, "test", JS::MK_NUMBER(32))
  7. JS::DEF(myobj, "test", JS::WRITABLE(FALSE) OR JS::PRESERVE_VALUE(),JS::MK_NULL())
  8. JS::SETS(myobj, "test", JS::MK_NUMBER(16))
  9. PRINT "test = ",JS::GET_INT(JS::GETS(myobj, "test")),"\n"
  10. JS::DESTROY
  11.  


jrs@jrs-laptop:~/sb/examples/js$ scriba js_deftest.sb
test = 32
jrs@jrs-laptop:~/sb/examples/js$


Load .js file and get properties
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. JS::CREATE
  4. JS::EXEC_FILE "properties.js"
  5. propcnt = JS::GET_PROPERTIES(JS::GETS(JS::SYS,"test"), proparray)
  6. PRINT "Property\tValue\tAttribute\n"
  7. FOR i = 1 to propcnt
  8.   PRINT proparray[i,0],"\t\t",proparray[i,1],"\t",proparray[i,2],"\n"
  9. NEXT
  10. JS::DESTROY
  11.  

properties.js
Code: Javascript
  1. var test = {};
  2.  
  3. Object.defineProperty(test, 'a', {
  4.   value: 1,
  5.   writable: true,
  6.   enumerable: true,
  7.   configurable: true
  8. });
  9.  
  10. Object.defineProperty(test, 'b', {
  11.   value: 2,
  12.   writable: false,
  13.   enumerable: false,
  14.   configurable: false
  15. });
  16.  


jrs@jrs-laptop:~/sb/examples/js$ scriba js_propfile.sb
Property   Value   Attribute
b      2   7
a      1   0
jrs@jrs-laptop:~/sb/examples/js$


Call JavaScript function
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. jscode = """
  4. var sum = function(a, b, c) {
  5.  print (c);
  6.  return a + b; };
  7. """
  8.  
  9. JS::CREATE()
  10. JS::EXEC(jscode)
  11. func = JS::GETS(JS::SYS, "sum")
  12. args = JS::MK_ARRAY()
  13. JS::ARRAY_PUSH(args, JS::MK_NUMBER(123.0))
  14. JS::ARRAY_PUSH(args, JS::MK_NUMBER(0.456))
  15. JS::ARRAY_PUSH(args, JS::MK_STRING("Script BASIC"))
  16. result = JS::APPLY(func, 0, args, rcode)
  17. PRINT FORMAT("Result: %g\n", JS::GET_DOUBLE(result))
  18. JS::DESTROY
  19.  


jrs@jrs-laptop:~/sb/examples/js$ scriba js_callfunc.sb
Script BASIC
Result: 123.456
jrs@jrs-laptop:~/sb/examples/js$


Describe Properties with JavaScript
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. ' Create JavaScript instance
  4. JS::CREATE
  5.  
  6. ' Create JavaScript object
  7. JS::EXEC("var myobj = {};")
  8. myobj = JS::GETS(JS::SYS, "myobj")
  9.  
  10. ' Create object property
  11. JS::DEF(myobj, "test", JS::WRITABLE(TRUE), JS::MK_NUMBER(64))
  12.  
  13. ' Describe object properties with JavaScript
  14. jscode = """
  15. var descriptors = {};
  16.  
  17. Object.keys(myobj).forEach(function(key) {
  18.    descriptors[key] = Object.getOwnPropertyDescriptor(myobj, key);
  19. });
  20.  
  21. var objdesc = JSON.stringify(descriptors);
  22. """
  23. JS::EXEC(jscode)
  24.  
  25. ' Return JSON formatted result string
  26. PRINT JS::GET_STRING(JS::GETS(JS::SYS, "objdesc")),"\n"
  27.  
  28. ' Release JavaScript instance
  29. JS::DESTROY
  30.  


jrs@jrs-laptop:~/sb/examples/js$ scriba js_defobj.sb
{"test":{"configurable":true,"enumerable":true,"writable":true,"value":64}}
jrs@jrs-laptop:~/sb/examples/js$


JSON / Stringify
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. JS::CREATE
  4. myobj = JS::MK_OBJECT()
  5. JS::DEF(myobj, "myprop_1", 0, JS::MK_NUMBER(64))
  6. JS::DEF(myobj, "myprop_2", 0, JS::MK_NUMBER(1.23))
  7. JS::DEF(myobj, "myprop_3", 0, JS::MK_STRING("JavaScript"))
  8. PRINT JS::STRINGIFY(myobj, JS::JSON),"\n"
  9. JS::DESTROY
  10.  


jrs@jrs-laptop:~/sb/examples/js$ scriba js_stringify.sb
{"myprop_3":"JavaScript","myprop_2":1.23,"myprop_1":64}
jrs@jrs-laptop:~/sb/examples/js$


JavaScript Regular Expression
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. jscode = """
  4. var output = ['---------- Original String\\n', names + '\\n'];
  5.  
  6. // Prepare two regular expression patterns and array storage.
  7. // Split the string into array elements.
  8.  
  9. // pattern: possible white space then semicolon then possible white space
  10. var pattern = /\\s*;\\s*/;
  11.  
  12. // Break the string into pieces separated by the pattern above and
  13. // store the pieces in an array called nameList
  14. var nameList = names.split(pattern);
  15.  
  16. // new pattern: one or more characters then spaces then characters.
  17. // Use parentheses to "memorize" portions of the pattern.
  18. // The memorized portions are referred to later.
  19. pattern = /(\\w+)\\s+(\\w+)/;
  20.  
  21. // New array for holding names being processed.
  22. var bySurnameList = [];
  23.  
  24. // Display the name array and populate the new array
  25. // with comma-separated names, last first.
  26. //
  27. // The replace method removes anything matching the pattern
  28. // and replaces it with the memorized string—second memorized portion
  29. // followed by comma space followed by first memorized portion.
  30. //
  31. // The variables $1 and $2 refer to the portions
  32. // memorized while matching the pattern.
  33.  
  34. output.push('---------- After Split by Regular Expression');
  35.  
  36. var i, len;
  37. for (i = 0, len = nameList.length; i < len; i++) {
  38.  output.push(nameList[i]);
  39.  bySurnameList[i] = nameList[i].replace(pattern, '$2, $1');
  40. }
  41.  
  42. // Display the new array.
  43. output.push('---------- Names Reversed');
  44. for (i = 0, len = bySurnameList.length; i < len; i++) {
  45.  output.push(bySurnameList[i]);
  46. }
  47.  
  48. // Sort by last name, then display the sorted array.
  49. bySurnameList.sort();
  50. output.push('---------- Sorted');
  51. for (i = 0, len = bySurnameList.length; i < len; i++) {
  52.  output.push(bySurnameList[i]);
  53. }
  54.  
  55. output.push('---------- End');
  56.  
  57. var retstr = output.join('\\n');
  58. """
  59.  
  60. ' The name string contains multiple spaces and tabs,
  61. ' and may have multiple spaces between first and last names.
  62.  
  63. JS::CREATE
  64. JS::EXEC("var names = 'Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand ';")
  65. JS::EXEC(jscode)
  66. PRINT JS::GET_STRING(JS::GETS(JS::SYS, "retstr"))
  67. JS::DESTROY
  68.  


jrs@jrs-laptop:~/sb/examples/js$ time scriba js_regexp.sb
---------- Original String

Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand

---------- After Split by Regular Expression
Harry Trump
Fred Barney
Helen Rigby
Bill Abel
Chris Hand
---------- Names Reversed
Trump, Harry
Barney, Fred
Rigby, Helen
Abel, Bill
Hand, Chris
---------- Sorted
Abel, Bill
Barney, Fred
Hand, Chris
Rigby, Helen
Trump, Harry
---------- End
real   0m0.030s
user   0m0.028s
sys   0m0.000s
jrs@jrs-laptop:~/sb/examples/js$


11
What's New / Script BASIC Windows 32 bit - Download Available
« on: February 14, 2017, 02:57:04 PM »
I have finally assembled an Inno install for Script BASIC for Windows 32 bit with extension modules and their dependencies included. This is my first public release in this format and would appreciate any feedback you're willing to offer to make Script BASIC even better for everyone.

If you plan on using the MySQL extension module or use the SBHTTPD proxy web server, I highly recommend installing the 32 bit version of the XAMPP package (and any of the other free packages they offer) for your local  database and Apache web server environment.

Script BASIC Examples and Source


Note: Download is currently being rebuilt to add new features. Check Back Again Soon.

12
Extension Modules / JavaScript Extension Module
« on: November 18, 2016, 10:34:36 AM »
The Script BASIC JS extension module is based on Cesanta's V7 JavaScript Engine. It claims to be the world's smallest footprint JavaScript 5.1 compatible embeddable engine. There are no other dependencies required. I have attached a Ubuntu 16.04 64 bit binary shared object (.so) and interface include file. The js.inc is briefly documented but hopefully the following examples will get you started. This is just a first round beta and I would appreciate any feedback or examples you may be willing to share.

js.inc
Code: Script BASIC
  1. MODULE JS
  2.  
  3. ' CORE
  4. DECLARE SUB      js_create                  ALIAS "js_create"                 LIB "js"
  5. DECLARE SUB      js_destroy                 ALIAS "js_destroy"                LIB "js"
  6. DECLARE SUB      js_get_global              ALIAS "js_get_global"             LIB "js"
  7. DECLARE SUB      js_get_this                ALIAS "js_get_this"               LIB "js"
  8. DECLARE SUB      js_get_arguments           ALIAS "js_get_arguments"          LIB "js"
  9. DECLARE SUB      js_arg                     ALIAS "js_arg"                    LIB "js"
  10. DECLARE SUB      js_argc                    ALIAS "js_argc"                   LIB "js"
  11. DECLARE SUB      js_own                     ALIAS "js_own"                    LIB "js"
  12. DECLARE SUB      js_disown                  ALIAS "js_disown"                 LIB "js"
  13. DECLARE SUB      js_set_gc_enabled          ALIAS "js_set_gc_enabled"         LIB "js"
  14. DECLARE SUB      js_interrupt               ALIAS "js_interrupt"              LIB "js"
  15. DECLARE SUB      js_get_parser_error        ALIAS "js_get_parser_error"       LIB "js"
  16.  
  17. ' PRIMITIVES
  18. DECLARE SUB      js_mk_number               ALIAS "js_mk_number"              LIB "js"
  19. DECLARE SUB      js_get_double              ALIAS "js_get_double"             LIB "js"
  20. DECLARE SUB      js_get_int                 ALIAS "js_get_int"                LIB "js"
  21. DECLARE SUB      js_is_number               ALIAS "js_is_number"              LIB "js"
  22. DECLARE SUB      js_mk_boolean              ALIAS "js_mk_boolean"             LIB "js"
  23. DECLARE SUB      js_get_bool                ALIAS "js_get_bool"               LIB "js"
  24. DECLARE SUB      js_is_boolean              ALIAS "js_is_boolean"             LIB "js"
  25. DECLARE SUB      js_mk_null                 ALIAS "js_mk_null"                LIB "js"
  26. DECLARE SUB      js_is_null                 ALIAS "js_is_null"                LIB "js"
  27. DECLARE SUB      js_mk_undefined            ALIAS "js_mk_undefined"           LIB "js"
  28. DECLARE SUB      js_is_undefined            ALIAS "js_is_undefined"           LIB "js"
  29. DECLARE SUB      js_mk_foreign              ALIAS "js_mk_foreign"             LIB "js"
  30. DECLARE SUB      js_get_ptr                 ALIAS "js_get_ptr"                LIB "js"
  31. DECLARE SUB      js_is_foreign              ALIAS "js_is_foreign"             LIB "js"
  32.  
  33. ' STRINGS
  34. DECLARE SUB      js_mk_string               ALIAS "js_mk_string"              LIB "js"
  35. DECLARE SUB      js_is_string               ALIAS "js_is_string"              LIB "js"
  36. DECLARE SUB      js_get_string              ALIAS "js_get_string"             LIB "js"
  37. DECLARE SUB      js_get_cstring             ALIAS "js_get_cstring"            LIB "js"
  38.  
  39. ' OBJECTS
  40. DECLARE SUB      js_mk_object               ALIAS "js_mk_object"              LIB "js"
  41. DECLARE SUB      js_is_object               ALIAS "js_is_object"              LIB "js"
  42. DECLARE SUB      js_get_proto               ALIAS "js_get_proto"              LIB "js"
  43. DECLARE SUB      js_set_proto               ALIAS "js_set_proto"              LIB "js"
  44. DECLARE SUB      js_get                     ALIAS "js_get"                    LIB "js"
  45. DECLARE SUB      js_def                     ALIAS "js_def"                    LIB "js"
  46. DECLARE SUB      js_set                     ALIAS "js_set"                    LIB "js"
  47. DECLARE SUB      js_del                     ALIAS "js_del"                    LIB "js"
  48. DECLARE SUB      js_init_prop_iter_ctx      ALIAS "js_init_prop_iter_ctx"     LIB "js"
  49. DECLARE SUB      js_next_prop               ALIAS "js_next_prop"              LIB "js"
  50. DECLARE SUB      js_destruct_prop_iter_ctx  ALIAS "js_destruct_prop_iter_ctx" LIB "js"
  51. DECLARE SUB      js_is_instanceof           ALIAS "js_is_instanceof"          LIB "js"
  52. DECLARE SUB      js_is_instanceof_v         ALIAS "js_is_instanceof_v"        LIB "js"
  53.  
  54. ' ARRAYS
  55. DECLARE SUB      js_mk_array                ALIAS "js_mk_array"               LIB "js"
  56. DECLARE SUB      js_is_array                ALIAS "js_is_array"               LIB "js"
  57. DECLARE SUB      js_array_length            ALIAS "js_array_length"           LIB "js"
  58. DECLARE SUB      js_array_push              ALIAS "js_array_push"             LIB "js"
  59. DECLARE SUB      js_array_get               ALIAS "js_array_get"              LIB "js"
  60. DECLARE SUB      js_array_set               ALIAS "js_array_set"              LIB "js"
  61. DECLARE SUB      js_array_del               ALIAS "js_array_del"              LIB "js"
  62.  
  63. ' EXECUTION
  64. DECLARE SUB      js_exec                    ALIAS "js_exec"                   LIB "js"
  65. DECLARE SUB      js_exec_file               ALIAS "js_exec_file"              LIB "js"
  66. DECLARE SUB      js_apply                   ALIAS "js_apply"                  LIB "js"
  67. DECLARE SUB      js_parse_json              ALIAS "js_parse_json"             LIB "js"
  68. DECLARE SUB      js_parse_json_file         ALIAS "js_parse_json_file"        LIB "js"
  69.  
  70. 'REGEX
  71. DECLARE SUB      js_mk_regexp               ALIAS "js_mk_regexp"              LIB "js"
  72. DECLARE SUB      js_is_regexp               ALIAS "js_is_regexp"              LIB "js"
  73.  
  74. ' UTILITY
  75. DECLARE SUB      js_stringify               ALIAS "js_stringify"              LIB "js"
  76. DECLARE SUB      js_println                 ALIAS "js_println"                LIB "js"
  77.  
  78. DECLARE SUB      SB_shifts                  ALIAS "SB_shifts"                 LIB "js"
  79. DECLARE COMMAND  js_iif                     ALIAS "js_iif"                    LIB "js"
  80.  
  81.  
  82. ' JS Global Module Variables
  83. OBJ = 0
  84. SYS = 0
  85.  
  86. ' Stringify Modes
  87. DEFAULT = 0
  88. JSON    = 1
  89. DEBUG   = 2
  90.  
  91. ' Property Attribute Support
  92.  
  93. CONST V7_PROPERTY_NON_WRITABLE              = 1
  94. CONST V7_PROPERTY_NON_ENUMERABLE            = 2
  95. CONST V7_PROPERTY_NON_CONFIGURABLE          = 4
  96. CONST V7_PROPERTY_GETTER                    = 8
  97. CONST V7_PROPERTY_SETTER                    = 16
  98. CONST _V7_PROPERTY_HIDDEN                   = 32
  99. CONST _V7_PROPERTY_OFF_HEAP                 = 64
  100. CONST _V7_PROPERTY_USER_DATA_AND_DESTRUCTOR = 128
  101. CONST _V7_DESC_PRESERVE_VALUE               = 256
  102. CONST _V7_DESC_MASK                         = &HFFFF
  103.  
  104. CONST PROPERTY_DEFAULT = 0
  105.  
  106. ' TRUE or FALSE. Whether the property's value can be set.
  107. FUNCTION WRITABLE(v)
  108.   IF v THEN
  109.     WRITABLE = PROPERTY_DEFAULT
  110.   ELSE
  111.     WRITABLE = V7_PROPERTY_NON_WRITABLE
  112.   END IF
  113. END FUNCTION
  114.  
  115. ' TRUE or FALSE. Whether the property shows in some loop constructs.
  116. FUNCTION ENUMERABLE(v)
  117.   IF v THEN
  118.     ENUMERABLE = PROPERTY_DEFAULT
  119.   ELSE
  120.     ENUMERABLE = V7_PROPERTY_NON_ENUMERABLE
  121.   END IF
  122. END FUNCTION
  123.  
  124. ' TRUE or FALSE. Whether the property can be deleted and whether its attributes can be changed.
  125. FUNCTION CONFIGURABLE(v)
  126.   IF v THEN
  127.     CONFIGURABLE = PROPERTY_DEFAULT
  128.   ELSE
  129.     CONFIGURABLE = V7_PROPERTY_NON_CONFIGURABLE
  130.   END IF
  131. END FUNCTION
  132.  
  133. ' TRUE or FALSE. When a property is accessed the value is generated by calling a function implicitly.
  134. FUNCTION GETTER(v)
  135.   IF v THEN
  136.     GETTER = V7_PROPERTY_GETTER
  137.   ELSE
  138.     GETTER = FALSE
  139.   END IF
  140. END FUNCTION
  141.  
  142. ' TRUE or FALSE. When a property is set it will implicitly call a function and pass a
  143. ' value as argument, and the return value of the function is set to the property.
  144. FUNCTION SETTER(v)
  145.   IF v THEN
  146.     SETTER = V7_PROPERTY_SETTER
  147.   ELSE
  148.     SETTER = FALSE
  149.   END IF
  150. END FUNCTION
  151.  
  152. FUNCTION PRESERVE_VALUE
  153.     PRESERVE_VALUE = _V7_DESC_PRESERVE_VALUE
  154. END FUNCTION
  155.  
  156. FUNCTION HIDDEN(v)
  157.   IF v THEN
  158.     HIDDEN = V7_PROPERTY_HIDDEN
  159.   ELSE
  160.     HIDDEN = FALSE
  161.   END IF
  162. END FUNCTION
  163.  
  164. FUNCTION OFF_HEAP(v)
  165.   IF v THEN
  166.     OFF_HEAP = _V7_PROPERTY_OFF_HEAP
  167.   ELSE
  168.     OFF_HEAP = FALSE
  169.   END IF
  170. END FUNCTION
  171.  
  172.  
  173. ' JS API Function Wrappers
  174.  
  175. ' Create V7 instance
  176. FUNCTION CREATE
  177.   OBJ = js_create()
  178.   SYS = js_get_global(OBJ)
  179.   CREATE = OBJ
  180. END FUNCTION
  181.  
  182. ' Destroy V7 instance
  183. SUB DESTROY
  184.   js_destroy(OBJ)
  185.   UNDEF OBJ
  186. END SUB
  187.  
  188. ' Return root level (`global`) object of the given V7 instance
  189. FUNCTION GET_GLOBAL
  190.   GET_GLOBAL = js_get_global(OBJ)
  191. END FUNCTION
  192.  
  193. ' Return current `this` object
  194. FUNCTION GET_THIS
  195.   GET_THIS = js_get_this(OBJ)
  196. END FUNCTION
  197.  
  198. ' Return current `arguments` array
  199. FUNCTION GET_ARGUMENTS
  200.   GET_ARGUMENTS = js_get_arguments(OBJ)
  201. END FUNCTION
  202.  
  203. ' Return i-th argument
  204. FUNCTION ARG(i)
  205.   ARG = js_arg(OBJ, i)
  206. END FUNCTION
  207.  
  208. ' Return the length (`count`) of `arguments`
  209. FUNCTION ARGC
  210.   ARGC = js_argc(OBJ)
  211. END FUNCTION
  212.  
  213. ' Tells the GC about a JS value variable/field owned by `C` code.
  214. SUB OWN(v)
  215.   js_own(OBJ, v)
  216. END SUB
  217.  
  218. ' User code should also explicitly disown the variables with v7_disown
  219. ' once it goes out of scope or the structure containing the v7_val_t field is freed.
  220. ' Returns 1 if value is found, 0 otherwise
  221. FUNCTION DISOWN(v)
  222.   DISOWN = js_disown(OBJ, v)
  223. END FUNCTION
  224.  
  225. ' Enable or disable GC
  226. SUB SET_GC_ENABLED(enabled)
  227.   js_set_gc_enabled(OBJ, enabled)
  228. END SUB
  229.  
  230. ' It sets a flag that will cause the interpreter to throw an Interrupted Error
  231. SUB INTERRUPT
  232.   js_interrupt(OBJ)
  233. END SUB
  234.  
  235. ' Returns last parser error message
  236. FUNCTION GET_ERROR
  237.   GET_ERROR = js_get_parser_error(OBJ)
  238. END FUNCTION
  239.  
  240. ' Make numeric primitive value
  241. FUNCTION MK_NUMBER(num)
  242.   MK_NUMBER = js_mk_number(OBJ, num)
  243. END FUNCTION
  244.  
  245. ' Returns number value stored in `v7_val_t` as `double`
  246. FUNCTION GET_DOUBLE(v)
  247.   GET_DOUBLE = js_get_double(OBJ, v)
  248. END FUNCTION
  249.  
  250. ' Returns number value stored in `v7_val_t` as `int`. If the number
  251. ' value is not an integer, the fraction part will be discarded.
  252. FUNCTION GET_INT(v)
  253.   GET_INT = js_get_int(OBJ, v)
  254. END FUNCTION
  255.  
  256. ' Returns true if given value is a primitive number value
  257. FUNCTION IS_NUMBER(v)
  258.   IS_NUMBER = js_is_number(v)
  259. END FUNCTION
  260.  
  261. ' Make boolean primitive value (either `true` or `false`)
  262. FUNCTION MK_BOOLEAN(is_true)
  263.   MK_BOOLEAN = js_mk_boolean(OBJ, is_true)
  264. END FUNCTION
  265.  
  266. ' Returns boolean stored in `v7_val_t`: 0 for `false` or
  267. ' non-boolean, non-0 for `true`
  268. FUNCTION GET_BOOL(v)
  269.   GET_BOOL = js_get_bool(OBJ, v)
  270. END FUNCTION
  271.  
  272. ' Returns `true` if given value is a primitive boolean value
  273. FUNCTION IS_BOOLEAN(v)
  274.   IS_BOOLEAN = js_is_boolean(v)
  275. END FUNCTION
  276.  
  277. ' Make `null` primitive value
  278. FUNCTION MK_NULL
  279.   MK_NULL = js_mk_null()
  280. END FUNCTION
  281.  
  282. ' Returns true if given value is a primitive `null` value
  283. FUNCTION IS_NULL(v)
  284.   IS_NULL = js_is_null(v)
  285. END FUNCTION
  286.  
  287. ' Make `undefined` primitive value
  288. FUNCTION MK_UNDEFINED
  289.   MK_UNDEFINED = js_mk_undefined()
  290. END FUNCTION
  291.  
  292. ' Returns true if given value is a primitive `undefined` value
  293. FUNCTION IS_UNDEFINED(v)
  294.   IS_UNDEFINED = js_is_undefined(v)
  295. END FUNCTION
  296.  
  297. ' Make JavaScript value that holds C/C++ `void *` pointer
  298. FUNCTION MK_FOREIGN
  299.   MK_FOREIGN = js_mk_foreign(OBJ)
  300. END FUNCTION
  301.  
  302. ' Returns `void *` pointer stored in `v7_val_t`
  303. ' Returns NULL `undef` if the value is not a foreign pointer
  304. FUNCTION GET_PTR(v)
  305.   GET_PTR = js_get_ptr(OBJ, v)
  306. END FUNCTION
  307.  
  308. ' Returns true if given value holds `void *` pointer
  309. FUNCTION IS_FOREIGN(v)
  310.   IS_FOREIGN = js_is_foreign(v)
  311. END FUNCTION
  312.  
  313. ' Creates a string primitive value
  314. FUNCTION MK_STRING(strval)
  315.   MK_STRING = js_mk_string(OBJ, strval, LEN(strval), 1)
  316. END FUNCTION
  317.  
  318. ' Returns true if given value is a primitive string value
  319. FUNCTION IS_STRING(v)
  320.   IS_STRING = js_is_string(v)
  321. END FUNCTION
  322.  
  323. ' Returns a pointer to the string stored in `v7_val_t`
  324. FUNCTION GET_STRING(v)
  325.   GET_STRING = js_get_string(OBJ, v)
  326. END FUNCTION
  327.  
  328. ' Returns a pointer to the string stored in `v7_val_t`
  329. ' Returns NULL `undef` if the value is not a string or
  330. ' if the string is not compatible with a C string
  331. FUNCTION GET_CSTRING(v)
  332.   GET_CSTRING = js_get_cstring(OBJ, v)
  333. END FUNCTION
  334.  
  335. ' Make an empty object
  336. FUNCTION MK_OBJECT
  337.   MK_OBJECT = js_mk_object(OBJ)
  338. END FUNCTION
  339.  
  340. ' Returns true if the given value is an object or function
  341. FUNCTION IS_OBJECT(v)
  342.   IS_OBJECT = js_is_object(v)
  343. END FUNCTION
  344.  
  345. ' Get object's prototype.
  346. FUNCTION GET_PROTO(object)
  347.   GET_PROTO = js_get_proto(OBJ, object)
  348. END FUNCTION
  349.  
  350. ' Set object's prototype. Return old prototype or undefined on error
  351. FUNCTION SET_PROTO(object, proto)
  352.   SET_PROTO = js_set_proto(OBJ, object, proto)
  353. END FUNCTION
  354.  
  355. ' Lookup property `name` in object `obj`. If `obj` holds no such property,
  356. ' an `undefined` value is returned
  357. FUNCTION GETS(object, objname)
  358.   GETS = js_get(OBJ, object, objname, LEN(objname))
  359. END FUNCTION
  360.  
  361. ' Define object property, similar to JavaScript `Object.defineProperty()`
  362. FUNCTION DEF(object, objname, attr, value)
  363.   DEF = js_def(OBJ, object, objname, LEN(objname), attr, value)
  364. END FUNCTION
  365.  
  366. ' Set object property. Behaves just like JavaScript assignment
  367. FUNCTION SETS(object, objname, value)
  368.   SETS = js_set(OBJ, object, objname, LEN(objname), value)
  369. END FUNCTION
  370.  
  371. ' Delete own property `name` of the object `obj`
  372. ' Does not follow the prototype chain
  373. FUNCTION DEL(object, objname)
  374.   DEL = js_del(OBJ, object, objname, LEN(objname))
  375. END FUNCTION
  376.  
  377. ' Returns true if the object is an instance of a given constructor / class name
  378. FUNCTION IS_INSTANCEOF(object, classname)
  379.   IS_INSTANCEOF = js_is_instanceof(OBJ, object, classname)
  380. END FUNCTION
  381.  
  382. ' Returns true if the object is an instance of a given constructor object class
  383. FUNCTION IS_INSTANCEOF_V(object, objclass)
  384.   IS_INSTANCEOF_V = js_is_instanceof_v(OBJ, object, objclass)
  385. END FUNCTION
  386.  
  387. ' Custom multi-property `GET` function
  388. FUNCTION GET_PROPERTIES(object, proparray)
  389.   LOCAL objname, value, attr, propcnt
  390.   objname = ""
  391.   value = 0
  392.   attr = 0
  393.   propcnt = 1
  394.  ' UNDEF proparray
  395.  js_init_prop_iter_ctx(OBJ, object)
  396.   WHILE js_next_prop(OBJ, objname, value, attr) <> undef
  397.     proparray[propcnt, 0] = js_get_string(OBJ, objname)
  398.     proparray[propcnt, 1] = js_get_int(OBJ, value)
  399.     proparray[propcnt, 2] = attr
  400.     propcnt += 1
  401.   WEND
  402.   js_destruct_prop_iter_ctx(OBJ)
  403.   GET_PROPERTIES = propcnt - 1
  404. END FUNCTION
  405.  
  406. ' Make an empty array object
  407. FUNCTION MK_ARRAY
  408.   MK_ARRAY = js_mk_array(OBJ)
  409. END FUNCTION
  410.  
  411. ' Returns true if given value is an array object
  412. FUNCTION IS_ARRAY(object)
  413.   IS_ARRAY = js_is_array(OBJ, object)
  414. END FUNCTION
  415.  
  416. ' Returns length on an array. If `object` is not an array, 0 is returned
  417. FUNCTION ARRAY_LENGTH(object)
  418.   ARRAY_LENGTH = js_array_length(OBJ, object)
  419. END FUNCTION
  420.  
  421. ' Insert `value` in `object` at the end of the array
  422. FUNCTION ARRAY_PUSH(object, value)
  423.   ARRAY_PUSH = js_array_push(OBJ, object, value)
  424. END FUNCTION
  425.  
  426. '  Return array member at index `index`. If `index` is out of bounds, undefined is returned
  427. FUNCTION ARRAY_GET(object, index)
  428.   ARRAY_GET = js_array_get(OBJ, object, index)
  429. END FUNCTION
  430.  
  431. ' Insert value `v` into `arr` at 'index`
  432. FUNCTION ARRAY_SET(object, index, value)
  433.   ARRAY_SET = js_array_set(OBJ, object, index, value)
  434. END FUNCTION
  435.  
  436. ' Delete value in array `arr` at index `index`, if it exists
  437. SUB ARRAY_DEL(object, index)
  438.    js_array_del(OBJ, object, index)
  439. END SUB
  440.  
  441. ' Execute JavaScript `js_code`
  442. ' The result of evaluation is stored in the `return` variable
  443. ' The 'ok' argument will contain the function's execution success status flag
  444. FUNCTION EXEC(code, ok)
  445.   EXEC = js_exec(OBJ, code, ok)
  446. END FUNCTION
  447.  
  448. ' Same as `v7_exec()`, but loads source code from `path` file
  449. FUNCTION EXEC_FILE(codepath, ok)
  450.   EXEC_FILE = js_exec_file(OBJ, codepath, ok)
  451. END FUNCTION
  452.  
  453. ' Parse `json_code`
  454. ' The result of evaluation is stored in the `return` variable
  455. ' The 'ok' argument will contain the function's parse success status flag
  456. FUNCTION PARSE_JSON(json_code, ok)
  457.   PARSE_JSON = js_parse_json(OBJ, json_code, ok)
  458. END FUNCTION
  459.  
  460. ' Same as `v7_parse_json()`, but loads `json_code` from `path` file
  461. FUNCTION PARSE_JSON_FILE(json_code_file, ok)
  462.   PARSE_JSON_FILE = js_parse_json_file(OBJ, json_code_file, ok)
  463. END FUNCTION
  464.  
  465. ' Call function `func` with arguments `args`, using `object` as `this`
  466. ' `args` should be an array containing arguments or `undefined`
  467. FUNCTION APPLY(func, object, args)
  468.   APPLY = js_apply(OBJ, func, object, args)
  469. END FUNCTION
  470.  
  471. ' Make RegExp object. For example, `regex` is `(.+)`, `flags` is `gi`.
  472. FUNCTION MK_REGEXP(regex, flags, rcode)
  473.   MK_REGEXP = js_mk_regexp(OBJ, regex, LEN(regex), flags, LEN(flags), rcode)
  474. END FUNCTION
  475.  
  476. ' Returns true if given value is a JavaScript RegExp object
  477. FUNCTION IS_REGEXP(object)
  478.   IS_REGEXP = js_is_regexp(OBJ, object)
  479. END FUNCTION
  480.  
  481. ' Generate string representation of the JavaScript value
  482. FUNCTION STRINGIFY(object, convtype)
  483.   STRINGIFY = js_stringify(OBJ, object, convtype)
  484. END FUNCTION
  485.  
  486. ' Output a string representation of the value to stdout followed by a newline
  487. SUB PRINTIFY(object)
  488.   js_println(OBJ, object)
  489. END SUB
  490.  
  491.  
  492. END MODULE
  493.  


Hello JavaScript
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. JS::CREATE
  4. PRINT JS::GET_INT(JS::EXEC("1 + 1")),"\n"
  5. JS::DESTROY
  6.  


jrs@jrs-laptop:~/sb/examples/js$ scriba js_hello2.sb
2
jrs@jrs-laptop:~/sb/examples/js$


Fibonacci
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. jscode = """
  4. function fibonacci(n) {
  5.  if (n <= 2) {
  6.    return 1;
  7.  } else {
  8.    return fibonacci(n - 1) + fibonacci(n - 2);
  9.  }
  10. }
  11.  
  12. print(fibonacci(24));
  13. """
  14.  
  15. JS::CREATE
  16. JS::EXEC(jscode)
  17. JS::DESTROY
  18.  


jrs@jrs-laptop:~/sb/examples/js$ scriba js_fibonacci.sb
46368
jrs@jrs-laptop:~/sb/examples/js$


Create object, property and attributes
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. JS::CREATE
  4. myobj = JS::MK_OBJECT()
  5. JS::DEF(myobj, "test", 0, JS::MK_NUMBER(64))
  6. JS::SETS(myobj, "test", JS::MK_NUMBER(32))
  7. JS::DEF(myobj, "test", JS::WRITABLE(FALSE) OR JS::PRESERVE_VALUE(),JS::MK_NULL())
  8. JS::SETS(myobj, "test", JS::MK_NUMBER(16))
  9. PRINT "test = ",JS::GET_INT(JS::GETS(myobj, "test")),"\n"
  10. JS::DESTROY
  11.  


jrs@jrs-laptop:~/sb/examples/js$ scriba js_deftest.sb
test = 32
jrs@jrs-laptop:~/sb/examples/js$


Load .js file and get properties
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. JS::CREATE
  4. JS::EXEC_FILE "properties.js"
  5. propcnt = JS::GET_PROPERTIES(JS::GETS(JS::SYS,"test"), proparray)
  6. PRINT "Property\tValue\tAttribute\n"
  7. FOR i = 1 to propcnt
  8.   PRINT proparray[i,0],"\t\t",proparray[i,1],"\t",proparray[i,2],"\n"
  9. NEXT
  10. JS::DESTROY
  11.  

properties.js
Code: Javascript
  1. var test = {};
  2.  
  3. Object.defineProperty(test, 'a', {
  4.   value: 1,
  5.   writable: true,
  6.   enumerable: true,
  7.   configurable: true
  8. });
  9.  
  10. Object.defineProperty(test, 'b', {
  11.   value: 2,
  12.   writable: false,
  13.   enumerable: false,
  14.   configurable: false
  15. });
  16.  


jrs@jrs-laptop:~/sb/examples/js$ scriba js_propfile.sb
Property   Value   Attribute
b      2   7
a      1   0
jrs@jrs-laptop:~/sb/examples/js$


Call JavaScript function
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. jscode = """
  4. var sum = function(a, b, c) {
  5.  print (c);
  6.  return a + b; };
  7. """
  8.  
  9. JS::CREATE()
  10. JS::EXEC(jscode)
  11. func = JS::GETS(JS::SYS, "sum")
  12. args = JS::MK_ARRAY()
  13. JS::ARRAY_PUSH(args, JS::MK_NUMBER(123.0))
  14. JS::ARRAY_PUSH(args, JS::MK_NUMBER(0.456))
  15. JS::ARRAY_PUSH(args, JS::MK_STRING("Script BASIC"))
  16. result = JS::APPLY(func, 0, args, rcode)
  17. PRINT FORMAT("Result: %g\n", JS::GET_DOUBLE(result))
  18. JS::DESTROY
  19.  


jrs@jrs-laptop:~/sb/examples/js$ scriba js_callfunc.sb
Script BASIC
Result: 123.456
jrs@jrs-laptop:~/sb/examples/js$


JSON / Stringify
Code: Script BASIC
  1. IMPORT js.inc
  2.  
  3. JS::CREATE
  4. myobj = JS::MK_OBJECT()
  5. JS::DEF(myobj, "myprop_1", 0, JS::MK_NUMBER(64))
  6. JS::DEF(myobj, "myprop_2", 0, JS::MK_NUMBER(1.23))
  7. JS::DEF(myobj, "myprop_3", 0, JS::MK_STRING("JavaScript"))
  8. PRINT JS::STRINGIFY(myobj, JS::JSON),"\n"
  9. JS::DESTROY
  10.  


jrs@jrs-laptop:~/sb/examples/js$ scriba js_stringify.sb
{"myprop_3":"JavaScript","myprop_2":1.23,"myprop_1":64}
jrs@jrs-laptop:~/sb/examples/js$


FYI: - A Windows 32 bit version may materialize if I can get by the unexplainable issues on this platform. Windows isn't generally supported by Cesanta.


13
Business BASIC Migrations / ProvideX Linux 64 IUP
« on: June 10, 2015, 11:00:34 PM »
Here is an example of an online dictionary program written in ProvideX (PxBasic) for Ubuntu 64 bit and using the IUP (Portable User Interface) with an interface shared object I wrote.

Update: I included a screen shot of the Windows version of ProvideX and IUP. Theming isn't working but the goal was to show the same PVX code running native GUI on Linux and Windows.

Code: Text
  1. ! Online Dictionary - Px* + IUP
  2.  
  3. BEGIN
  4.  
  5. lib = DLL(ADDR "/home/jrs/pxbasic/pxbiup.so")
  6.  
  7. DIM servers$[0]
  8. servers$[0]="dict.org"
  9.  
  10. about$ = "ProvideX IUP Binding"
  11.  
  12. ! Initialize IUP
  13. ok = DLL(lib, "IupOpen")
  14.  
  15. ! Create main window
  16.  
  17. win = DLL(lib, "IupCreate", "dialog"+$00$)
  18.   ok = DLL(lib, "IupSetAttributes", win, "TITLE="+QUO+"Online Dictionary - ProvideX/IUP"+QUO+", SIZE=500x300"+$00$)
  19.   ok = DLL(lib, "PxBSetCallback", win,"CLOSE_CB"+$00$)
  20.  
  21. ! Create container to house ALL GUI objects
  22.  
  23. vbox = DLL(lib, "IupCreate", "vbox"+$00$)
  24.   ok = DLL(lib, "IupSetAttributes", vbox, "MARGIN=10x10"+$00$)
  25.  
  26. ! Create server panel
  27.  
  28. topBox = DLL(lib, "IupCreate", "hbox"+$00$)
  29.   ok = DLL(lib, "IupSetAttributes", topBox, "GAP=10"+$00$)
  30.   ok = DLL(lib, "IupAppend", vbox, topBox)
  31. serverFrame = DLL(lib, "IupCreate", "frame"+$00$)
  32.   ok = DLL(lib, "IupSetAttributes", serverFrame, "TITLE=Servers, EXPAND=YES"+$00$)
  33.   ok = DLL(lib, "IupAppend", topBox, serverFrame)
  34. serverBox = DLL(lib, "IupCreate", "hbox"+$00$)
  35.   ok = DLL(lib, "IupSetAttributes", serverBox, "GAP=5"+$00$)
  36.   ok = DLL(lib, "IupAppend", serverFrame, serverBox)
  37. serverCombo = DLL(lib, "IupCreate", "list"+$00$)
  38.   ok = DLL(lib, "IupSetAttributes", serverCombo, "DROPDOWN=YES, SIZE=120x, EXPAND=HORIZONTAL, VALUE=1"+$00$)
  39.   ok = DLL(lib, "IupAppend", serverBox, serverCombo)
  40.   ok = DLL(lib, "PxBSetCallback", serverCombo, "ACTION"+$00$)
  41. btnFetch = DLL(lib, "IupCreate", "button"+$00$)
  42.   ok = DLL(lib, "IupSetAttributes", btnFetch, "TITLE=Fetch, SIZE = 50x"+$00$)
  43.   ok = DLL(lib, "IupAppend", serverBox, btnFetch)
  44.   ok = DLL(lib, "PxBSetCallback", btnFetch, "ACTION"+$00$)
  45.  
  46. ! Create control panel
  47.  
  48. controlFrame = DLL(lib, "IupCreate", "frame"+$00$)
  49.   ok = DLL(lib, "IupSetAttributes", controlFrame, "TITLE=Controls"+$00$)
  50.   ok = DLL(lib, "IupAppend", topBox, controlFrame)
  51. controlBox = DLL(lib, "IupCreate", "hbox"+$00$)
  52.   ok = DLL(lib, "IupSetAttributes", controlBox, "GAP=5"+$00$)
  53.   ok = DLL(lib, "IupAppend", controlFrame, controlBox)
  54. btnAbout = DLL(lib, "IupCreate", "button"+$00$)
  55.   ok = DLL(lib, "IupSetAttributes", btnAbout, "TITLE=About, SIZE = 50x"+$00$)
  56.   ok = DLL(lib, "IupAppend", controlBox, btnAbout)
  57.   ok = DLL(lib, "PxBSetCallback", btnAbout, "ACTION"+$00$)
  58. btnClear = DLL(lib, "IupCreate", "button"+$00$)
  59.   ok = DLL(lib, "IupSetAttributes", btnClear, "TITLE=Clear, SIZE = 50x"+$00$)
  60.   ok = DLL(lib, "IupAppend", controlBox, btnClear)
  61.   ok = DLL(lib, "PxBSetCallback", btnClear, "ACTION"+$00$)
  62. btnExit = DLL(lib, "IupCreate", "button"+$00$)
  63.   ok = DLL(lib, "IupSetAttributes", btnExit, "TITLE=Exit, SIZE = 50x"+$00$)
  64.   ok = DLL(lib, "IupAppend", controlBox, btnExit)
  65.   ok = DLL(lib, "PxBSetCallback", btnExit,"ACTION"+$00$)
  66.  
  67. ! Create dictionary panel
  68.  
  69. dictFrame = DLL(lib, "IupCreate", "frame"+$00$)
  70.   ok = DLL(lib, "IupSetAttributes", dictFrame, "TITLE=Dictionaries"+$00$)
  71.   ok = DLL(lib, "IupAppend", vbox, dictFrame)
  72. serverList = DLL(lib, "IupCreate", "list"+$00$)
  73.   ok = DLL(lib, "IupSetAttributes", serverList, "EXPAND=YES, VISIBLELINES=1"+$00$)
  74.   ok = DLL(lib, "IupAppend", dictFrame, serverList)
  75.   ok = DLL(lib, "PxBSetCallback", serverList, "ACTION"+$00$)
  76.  
  77. ! Create text part
  78.  
  79. transFrame = DLL(lib, "IupCreate", "frame"+$00$)
  80.   ok = DLL(lib, "IupSetAttributes", transFrame, "TITLE=Translation"+$00$)
  81.   ok = DLL(lib, "IupAppend", vbox, transFrame)
  82. text = DLL(lib, "IupCreate", "text"+$00$)
  83.   ok = DLL(lib, "IupSetAttributes", text, "MULTILINE=YES, EXPAND=YES"+$00$)
  84.   ok = DLL(lib, "IupAppend", transFrame, text)
  85.  
  86. ! Create entry and search button
  87.  
  88. bottomBox = DLL(lib, "IupCreate", "hbox"+$00$)
  89.   ok = DLL(lib, "IupSetAttributes", bottomBox, "GAP=10"+$00$)
  90.   ok = DLL(lib, "IupAppend", vbox, bottomBox)
  91. label = DLL(lib, "IupCreate", "label"+$00$)
  92.   ok = DLL(lib, "IupSetAttributes", label, "TITLE="+QUO+"Enter Word to Search For:"+QUO+", SIZE=x12"+$00$)
  93.   ok = DLL(lib, "IupAppend", bottomBox, label)
  94. entry = DLL(lib, "IupCreate", "text"+$00$)
  95.   ok = DLL(lib, "IupSetAttributes", entry, "EXPAND=HORIZONTAL"+$00$)
  96.   ok = DLL(lib, "IupAppend", bottomBox, entry)
  97. btnSearch = DLL(lib, "IupCreate", "button"+$00$)
  98.   ok = DLL(lib, "IupSetAttributes", btnSearch, "TITLE=Search, SIZE=50x"+$00$)
  99.   ok = DLL(lib, "IupAppend", bottomBox, btnSearch)
  100.   ok = DLL(lib, "PxBSetCallback", btnSearch, "ACTION"+$00$)
  101. chkAll = DLL(lib, "IupCreate","toggle"+$00$)
  102.   ok = DLL(lib, "IupSetAttributes", chkAll, "TITLE=ALL, SIZE=x12"+$00$)
  103.   ok = DLL(lib, "IupAppend", bottomBox, chkAll)
  104. chkUTF = DLL(lib, "IupCreate", "toggle"+$00$)
  105.   ok = DLL(lib, "IupSetAttributes", chkUTF, "TITLE=UTF-8, SIZE=x12"+$00$)
  106.   ok = DLL(lib, "IupAppend", bottomBox, chkUTF)
  107.  
  108. ! Add the main GUI container to the Window
  109.  
  110. ok = DLL(lib, "IupAppend", win, vbox)
  111.  
  112. ! Setup dialog defaults
  113.  
  114. ok = DLL(lib, "IupShow", win)
  115. ok = DLL(lib, "IupSetFocus", btnFetch)
  116. FOR i = 0 TO DIM(READ MAX(servers$))
  117.   ok = DLL(lib, "IupSetAttribute", serverCombo, "APPENDITEM"+$00$, servers$[i]+$00$)
  118. NEXT
  119. ok = DLL(lib, "IupSetAttribute", serverCombo, "VALUE"+$00$, "1"+$00$)
  120. ok = DLL(lib, "IupUpdate", serverCombo)
  121. server_selection$ = servers$[0]
  122.  
  123. ! Main processing loop
  124.  
  125. REPEAT
  126.   WAIT: ok = DLL(lib, "IupLoopStepWait")
  127.   this_event = DLL(lib, "GetEvent")
  128.   IF NOT(this_event) THEN GOTO WAIT
  129.   SWITCH this_event
  130.     CASE serverCombo
  131.       GOSUB serverCombo_selected
  132.       BREAK
  133.     CASE btnFetch
  134.       GOSUB btnFetch_clicked
  135.       BREAK
  136.     CASE btnAbout
  137.       GOSUB btnAbout_clicked
  138.       BREAK
  139.     CASE btnClear
  140.       GOSUB btnClear_clicked
  141.       BREAK
  142.     CASE serverList
  143.       GOSUB serverList_selected
  144.       BREAK
  145.     CASE btnSearch
  146.       GOSUB btnSearch_clicked
  147.       BREAK
  148.   END SWITCH
  149. UNTIL this_event = win OR this_event = btnExit
  150. ok = DLL(lib, "IupClose")
  151. ok = DLL(DROP lib)
  152. END
  153.  
  154. ! Callback routines
  155.  
  156. btnAbout_clicked:
  157.   ok = DLL(lib, "IupMessage", "ABOUT"+$00$, about$)
  158. RETURN
  159.  
  160. serverCombo_selected:
  161.   DIM server_selection$(1024)
  162.   ok = DLL(lib, "GetListSelectedText", server_selection$)
  163.   server_selection$ = STP(server_selection$, 2)
  164. RETURN
  165.  
  166. serverList_selected:
  167.   DIM whichDictionary$(1024)
  168.   ok = DLL(lib, "GetListSelectedText", whichDictionary$)
  169.   whichDictionary$ = STP(whichDictionary$, 2)
  170. RETURN
  171.  
  172. btnFetch_clicked:
  173.   dat$ = ""
  174.   DIM _total$[*]
  175.   count = 1
  176.   OPEN (1,BSZ=16384)"[TCP]"+server_selection$+";2628;NoDelay;stream"
  177.   WRITE (1)"SHOW DB"+$0D0A$
  178.   REPEAT
  179.     READ(1)raw_data$
  180.     dat$ = dat$ + raw_data$
  181.   UNTIL POS("250 ok" = raw_data$)
  182.   WRITE(1)"QUIT"+$0D0A$
  183.   CLOSE(1)
  184.   REPEAT
  185.     eol = POS($0D0A$=dat$)
  186.     _total$[count] = dat$(1,eol - 1)
  187.     dat$ = dat$(eol + 2)
  188.     count += 1
  189.   UNTIL dat$ = ""
  190.   FOR cnt = 3 TO count - 3
  191.     ok = DLL(lib, "IupSetAttribute", serverList, "APPENDITEM"+$00$, _total$[cnt]+$00$)
  192.    NEXT cnt
  193.   ok = DLL(lib, "IupSetAttribute", serverList, "APPENDITEM"+$00$, _total$[3]+$00$)
  194.   ok = DLL(lib, "IupSetAttribute", serverList, "VALUE"+$00$, "1"+$00$)
  195.   ok = DLL(lib, "IupUpdate", serverCombo)
  196.   whichDictionary$ = _total$[3]
  197. RETURN
  198.  
  199. G_NetError:
  200.   PRINT "Server ",server_selection$," not available. (",ERROR,")"
  201. RETURN
  202.  
  203. btnClear_clicked:
  204.   ok = DLL(lib, "IupSetAttribute", serverList,"1"+$00$)
  205.   ok = DLL(lib, "IupSetAttribute", text, "VALUE"+$00$, ""+$00$)
  206.   ok = DLL(lib, "IupSetAttribute", entry, "VALUE"+$00$, ""+$00$)
  207. RETURN
  208.  
  209. btnSearch_clicked:
  210.   dict$ = ""
  211.   dat$ = ""
  212.   total$ = ""
  213.   info$ = ""
  214.   ok = DLL(lib, "IupSetAttribute", text, "VALUE"+$00$, "Fetching...."+$00$)
  215.   dict$ = whichDictionary$(1, POS(" " = whichDictionary$) - 1)
  216.   OPEN (1,BSZ=16384)"[TCP]"+server_selection$+";2628;NoDelay;stream"
  217.   chkval$ = FNIupGetString$(DLL(lib, "IupGetAttribute", chkAll, "VALUE"+$00$))
  218.   IF chkval$ = "ON" THEN {
  219.     WRITE(1)"DEFINE * " + FNIupGetString$(DLL(lib, "IupGetAttribute", entry,"VALUE"+$00$)) + $0D0A$
  220.   } ELSE {
  221.     WRITE(1)"DEFINE " + dict$ + " " + FNIupGetString$(DLL(lib, "IupGetAttribute", entry,"VALUE"+$00$)) + $0D0A$
  222.   }
  223.   REPEAT
  224.     READ(1)raw_data$
  225.     data_str$ = data_str$ + raw_data$
  226.   UNTIL POS("250 ok [d/m/c =" = raw_data$)
  227.   WRITE(1)"QUIT"+$0D0A$
  228.   CLOSE(1)
  229.   ! Remove header
  230.   data_str$ = data_str$(POS($0D0A$=data_str$, 1, 2) + 2)
  231.   REPEAT
  232.     eol = POS($0D0A$=data_str$)
  233.     dat$ = data_str$(1,eol - 1)
  234.     data_str$ = data_str$(eol + 2)
  235.     IF dat$(1, 3) <> "151" THEN GOTO IT
  236.       entstr$ = FNIupGetString$(DLL(lib, "IupGetAttribute", entry, "VALUE"+$00$))
  237.       total$ = total$ + "------------------------------" + $0D0A$
  238.       total$ = total$ + dat$(2 + LEN(entstr$) + LEN(dict$)) + $0D0A$
  239.       total$ = total$ + "------------------------------"+ $0D0A$
  240.       REPEAT
  241.         eol = POS($0D0A$=data_str$)
  242.         info$ = data_str$(1,eol - 1)
  243.         data_str$ = data_str$(eol + 2)
  244.         info$ = SUB(info$, CHR(34), CHR(92) + CHR(34))
  245.         IF LEN(info$) AND info$(1, 1) <> "." THEN total$ += STP(info$, 2) + $0D0A$
  246.       UNTIL info$ > "" AND info$(1, 1) = "."
  247.       total$ += $0D0A$
  248.     IT:
  249.   UNTIL data_str$(1, 3) = "250" OR NUM(data_str$(1, 3)) > 499
  250.   IF dat$(1, 3) = "552" THEN total$ = "No match found."
  251.   IF dat$(1, 3) = "501" THEN total$ = "Select a dictionary first!"
  252.   IF dat$(1, 3) = "550" THEN total$ = "Invalid database!"
  253.   ok = DLL(lib, "IupSetAttribute", text, "VALUE"+$00$, total$+$00$)
  254. RETURN
  255.  
  256. L_NetError:
  257.   dat$ = "Could not lookup word! (" + STR(ERR) + ")"
  258.   ok = DLL(lib, "IupSetAttribute", text, "VALUE"+$00$, dat$+$00$)
  259. RETURN
  260.  
  261. ! DLL() return string emulator
  262.  
  263. DEF FNIupGetString$(LOCAL i_ptr)
  264.   LOCAL strlen, pxbuff$
  265.   DIM pxbuff$(4096)
  266.   strlen = DLL(lib, "ptr2pxbstr", i_ptr, pxbuff$)
  267.   RETURN pxbuff$(1,strlen)
  268. END DEF
  269.  


14
What's New / dbgcon - Script BASIC remote debugger
« on: May 05, 2015, 07:47:55 PM »
A few years ago I made some bug fixes to Peter's Verhas's experimental sdbg remote Script BASIC debugger. I wrote my own console client in SB which works great. I plan on using some of Dave Zimmer's enhancements to his VB/COM IDE/Debugger preprocessor project to view array contents and trace the call stack. The remote socket based Script BASIC debugger works with both local desktop scripts and remote sbhttpd proxy server web applications.

dbgcon.sb
Code: Script BASIC
  1. ' ScriptBasic Remote Console Debugger
  2.  
  3. cmdln = TRIM(COMMAND())
  4. IF cmdln = "" THEN
  5.   PRINT "Usage: dbgcon [prog2debug]\n"
  6.   END
  7. END IF
  8. exitcode = EXECUTE("/usr/bin/scriba -i sdbg " & cmdln,-1,PID)
  9. OPEN "127.0.0.1:6647" FOR SOCKET AS #1
  10. WHILE NOT EOF(1)
  11.   LINE INPUT #1, dbgs
  12.   IF dbgs = ".\n" THEN
  13.     PRINT "-> "
  14.     LINE INPUT dbgc
  15.     IF LCASE(CHOMP(dbgc)) = "h" THEN
  16. PRINT """h help
  17. s step one line
  18. S step one line, do not step into functions or subs
  19. o step until getting out of the current function
  20.  (if you stepped into but changed your mind)
  21. ? var  print the value of a variable
  22. u step one level up in the stack
  23. d step one level down in the stack (for variable printing)
  24. D step down in the stack to current execution depth
  25. G list all global variables
  26. L list all local variables
  27. l [n-m] list the source lines
  28. r [n] run to line n
  29. R [n] run to line n but do not stop in recursive function call
  30. b [n] set breakpoint on the line n or the current line
  31. B [n-m] remove breakpoints from lines
  32. q quit the program
  33. """
  34.     END IF
  35.     PRINT #1, dbgc
  36.     IF CHOMP(dbgc) = "q" THEN GOTO Done
  37.   ELSE
  38.     dbgcmd = CHOMP(dbgs)
  39. ' l - List Source  
  40.    IF INSTR(dbgcmd,"Break-Point: ")<>undef THEN
  41.       p = INSTR(dbgcmd,"Break-Point: ")
  42.       IF MID(dbgcmd,p+13,1) = "0" THEN
  43.         PRINT " "
  44.       ELSE
  45.         PRINT "*"
  46.       END IF
  47.       GOTO IT
  48.     END IF
  49.     IF INSTR(dbgcmd,"Line-Number: ")<>undef THEN
  50.       p = INSTR(dbgcmd,"Line-Number: ")
  51.       PRINT FORMAT("%~[0000] ~",VAL(MID(dbgcmd,p+13)))
  52.       online = TRUE
  53.       GOTO IT
  54.     END IF
  55.     IF INSTR(dbgcmd,"Line: ")<>undef THEN
  56.       p = INSTR(dbgcmd,"Line: ")
  57.       IF online THEN
  58.         PRINT MID(dbgcmd,p+6),"\n"
  59.       ELSE
  60.         PRINT MID(dbgcmd,p),"\n"
  61.       END IF        
  62.       online = FALSE
  63.       GOTO IT
  64.     END IF
  65.     IF INSTR(dbgcmd,"Global-Variable")<>undef THEN
  66.       p = INSTR(dbgcmd,"Global-Variable")
  67.       PRINT "G-Var" & MID(dbgcmd,p+15) & "\n"
  68.       GOTO IT
  69.     END IF
  70. ' Unprocessed out
  71.   PRINT dbgs
  72.   END IF
  73. IT:
  74. WEND
  75.  
  76. Done:
  77. PRINT #1,"q"
  78. CLOSE(1)
  79. PRINT "Debug session closed.\n"
  80. END
  81.  

testarray.sb
Code: Script BASIC
  1. ' Long / Double / String
  2. i = 1
  3. d = .99
  4. s = "JRS"
  5. ' Indices array
  6. a[0,0] = 0
  7. a[0,1] = 123
  8. a[0,2] = 1.23
  9. a[0,3] = "One,Two,Three"
  10. a[1,10] = "Zero"
  11. a[1,11] = 321
  12. a[1,12] = 32.1
  13. a[1,13] = "Three,Two,One"
  14. ' Asscociative array
  15. b{"One"} = 1
  16. b{"Two"} = .2
  17. b{"Three"} = "*3*"
  18. ' Mix asscociative & indices array
  19. c{"JRS"}[1] = 1
  20. c{"JRS"}[2] = .2
  21. c{"JRS"}[3] = "*3*"
  22. PRINT "Done\n"
  23.  

Output

jrs@laptop:~/sb/sb22/sbt$ scriba dbgcon.sb testarray.sb
Application: ScriptBasic Remote Debugger - Linux
Version: 1.0
Source-File-Count: 1
Source-File: testarray.sb
Line: 2
-> b15
Message: done
Line: 2
-> l1-
 [0001] ' Long / Double / String
 [0002] i = 1
 [0003] d = .99
 [0004] s = "JRS"
 [0005] ' Indices array
 [0006] a[0,0] = 0
 [0007] a[0,1] = 123
 [0008] a[0,2] = 1.23
 [0009] a[0,3] = "One,Two,Three"
 [0010] a[1,10] = "Zero"
 [0011] a[1,11] = 321
 [0012] a[1,12] = 32.1
 [0013] a[1,13] = "Three,Two,One"
 [0014] ' Asscociative array
*[0015] b{"One"} = 1
 [0016] b{"Two"} = .2
 [0017] b{"Three"} = "*3*"
 [0018] ' Mix asscociative & indices array
 [0019] c{"JRS"}[1] = 1
 [0020] c{"JRS"}[2] = .2
 [0021] c{"JRS"}[3] = "*3*"
 [0022] PRINT "Done\n"
Line: 2
-> r
Line: 15
-> r22
Line: 22
-> G
G-Var-Name: VT=0 @ 0x014BBD18 VN=main::i
G-Var-Value: 1
G-Var-Name: VT=1 @ 0x014C9258 VN=main::d
G-Var-Value: 0.990000
G-Var-Name: VT=2 @ 0x014C92B8 VN=main::s
G-Var-Value: "JRS"
G-Var-Name: VT=3 @ 0x014C9378 LB=0 : UB=1 VN=main::a
G-Var-Value: 
LB=0 : UB=3 VN=[0]
[0] VT=3 @ 0x014C9468 
[0] VT=0 @ 0x014C9558 0
[1] VT=0 @ 0x014C96D8 123
[2] VT=1 @ 0x014C9898 1.230000
[3] VT=2 @ 0x014BC0A8 "One,Two,Three"
LB=10 : UB=13 VN=[1]
[1] VT=3 @ 0x014BC1C8 
[10] VT=2 @ 0x014BC228 "Zero"
[11] VT=0 @ 0x014BC3A8 321
[12] VT=1 @ 0x014BC568 32.100000
[13] VT=2 @ 0x014BC6D8 "Three,Two,One"
G-Var-Name: VT=3 @ 0x014BC798 LB=0 : UB=5 VN=main::b
G-Var-Value: 
[0] VT=2 @ 0x014BC7F8 "One"
[1] VT=0 @ 0x014BC8B8 1
[2] VT=2 @ 0x014BC968 "Two"
[3] VT=1 @ 0x014BCA28 0.200000
[4] VT=2 @ 0x014BCAE8 "Three"
[5] VT=2 @ 0x014BCB48 "*3*"
G-Var-Name: VT=3 @ 0x014BCBA8 LB=0 : UB=1 VN=main::c
G-Var-Value: 
[0] VT=2 @ 0x014BCC08 "JRS"
LB=1 : UB=3 VN=[1]
[1] VT=3 @ 0x014BCCC8 
[1] VT=0 @ 0x014BCD88 1
[2] VT=1 @ 0x014BCEA8 0.200000
[3] VT=2 @ 0x014BD5C8 "*3*"
Line: 22
-> ?s
Value: "JRS"
Line: 22
-> ?b
Value: 
[0] VT=2 @ 0x014BC7F8 "One"
[1] VT=0 @ 0x014BC8B8 1
[2] VT=2 @ 0x014BC968 "Two"
[3] VT=1 @ 0x014BCA28 0.200000
[4] VT=2 @ 0x014BCAE8 "Three"
[5] VT=2 @ 0x014BCB48 "*3*"
Line: 22
-> r
Done
Debug session closed.
jrs@laptop:~/sb/sb22/sbt$


15
What's New / SBT - Script BASIC Tutorial API extension module
« on: May 05, 2015, 06:14:26 PM »
I have embedded Script BASIC into itself as an easy to use example of the embedding and extension API's. I used the C BASIC C preprocessor defines to extend Script BASIC's extensive macro and define definitions in the interface.c design for readability.

Current SBT Download Attached


SBT interface.c
Code: C
  1. /*  SBT (Script BASIC Tutorial) - Extension Module */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <math.h>
  8. #include <time.h>
  9. #include <unistd.h>
  10. #include "../../basext.h"
  11. #include "../../scriba.h"
  12. #include "cbasic.h"
  13.  
  14.  
  15. /****************************
  16.  Extension Module Functions
  17. ****************************/
  18.  
  19. besVERSION_NEGOTIATE
  20.   RETURN_FUNCTION((int)INTERFACE_VERSION);
  21. besEND
  22.  
  23. besSUB_START
  24.   DIM AS long PTR p;
  25.   besMODULEPOINTER = besALLOC(sizeof(long));
  26.   IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
  27.   p = (long PTR)besMODULEPOINTER;
  28.   RETURN_FUNCTION(0);
  29. besEND
  30.  
  31. besSUB_FINISH
  32.   DIM AS long PTR p;
  33.   p = (long PTR)besMODULEPOINTER;
  34.   IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
  35.   RETURN_FUNCTION(0);
  36. besEND
  37.  
  38.  
  39. /**********************
  40.  Script BASIC Instance
  41. **********************/
  42.  
  43. /******************
  44.  Support Routines
  45. ******************/
  46.  
  47. struct _RunServiceProgram {
  48.   char *pszProgramFileName;
  49.   char *pszCmdLineArgs;
  50.   char *pszConfigFileName;
  51.   pSbProgram pTProgram;
  52.   int iRestart;
  53.   };
  54.  
  55. static void ExecuteProgramThread(void *p){
  56.   pSbProgram pProgram;
  57.   char szInputFile[1024];
  58.   int iErrorCode;
  59.   struct _RunServiceProgram *pRSP;
  60.   pRSP = p;
  61.   strcpy(szInputFile,pRSP->pszProgramFileName);
  62.   pProgram = scriba_new(malloc,free);
  63.   pRSP->pTProgram = pProgram;
  64.   if( pProgram == NULL )return;
  65.   scriba_SetFileName(pProgram,szInputFile);
  66.   if (pRSP->pszConfigFileName != NULL){
  67.         strcpy(szInputFile,pRSP->pszConfigFileName);
  68.         scriba_LoadConfiguration(pProgram, pRSP->pszConfigFileName);
  69.   }else{
  70.         scriba_SetProcessSbObject(pProgram,pProgram);
  71.   }    
  72.   scriba_LoadSourceProgram(pProgram);
  73.   if (pRSP->pszCmdLineArgs != NULL){
  74.         strcpy(szInputFile,pRSP->pszCmdLineArgs);
  75.     iErrorCode = scriba_Run(pProgram,pRSP->pszCmdLineArgs);
  76.   }else{
  77.     iErrorCode = scriba_Run(pProgram,NULL);
  78.   }    
  79. //  scriba_destroy(pProgram);
  80.   return;
  81. }
  82.  
  83. besFUNCTION(SB_New)
  84.   DIM AS pSbProgram sbobj;
  85.   sbobj = scriba_new(malloc,free);
  86.   besRETURN_LONG(sbobj);
  87. besEND
  88.  
  89. besFUNCTION(SB_Configure)
  90.   DIM AS unsigned long sbobj;
  91.   DIM AS char PTR cfgfilename;
  92.   DIM AS int rtnval = -1;
  93.   besARGUMENTS("iz")
  94.     AT sbobj, AT cfgfilename
  95.   besARGEND
  96.   rtnval = scriba_LoadConfiguration(sbobj, cfgfilename);
  97.   besRETURN_LONG(rtnval);
  98. besEND
  99.  
  100. besFUNCTION(SB_Load)
  101.   DIM AS unsigned long sbobj;
  102.   DIM AS char PTR sbfilename;
  103.   DIM AS int rtnval = -1;
  104.   besARGUMENTS("iz")
  105.     AT sbobj, AT sbfilename
  106.   besARGEND
  107.   rtnval = scriba_SetFileName(sbobj, sbfilename);
  108.   scriba_LoadSourceProgram(sbobj);
  109.   besRETURN_LONG(rtnval);
  110. besEND
  111.  
  112. besFUNCTION(SB_LoadStr)
  113.   DIM AS unsigned long sbobj;
  114.   DIM AS char PTR sbpgm;
  115.   DIM AS int rtnval = -1;
  116.   besARGUMENTS("iz")
  117.     AT sbobj, AT sbpgm
  118.   besARGEND
  119.   scriba_SetFileName(sbobj, "fake");
  120.   rtnval = scriba_LoadProgramString(sbobj, sbpgm, strlen(sbpgm));
  121.   besRETURN_LONG(rtnval);
  122. besEND
  123.  
  124. besFUNCTION(SB_Run)
  125.   DIM AS unsigned long sbobj;
  126.   DIM AS int rtnval;
  127.   DIM AS char PTR sbcmdline;
  128.   besARGUMENTS("iz")
  129.     AT sbobj, AT sbcmdline
  130.   besARGEND
  131.   IF (besARGNR < 2) THEN_DO sbcmdline = "";
  132.   rtnval = scriba_Run(sbobj, sbcmdline);
  133.   besRETURN_LONG(rtnval);
  134. besEND
  135.  
  136. besFUNCTION(SB_NoRun)
  137.   DIM AS unsigned long sbobj;
  138.   DIM AS int rtnval;
  139.   besARGUMENTS("i")
  140.     AT sbobj
  141.   besARGEND
  142.   rtnval = scriba_NoRun(sbobj);
  143.   besRETURN_LONG(rtnval);
  144. besEND
  145.  
  146. besFUNCTION(SB_ThreadStart)
  147.   DIM AS struct _RunServiceProgram PTR pRSP;
  148.   DIM AS THREADHANDLE T;
  149.   DIM AS char PTR pszProgramFileName;
  150.   DIM AS char PTR pszCmdLineArgs;
  151.   DIM AS char PTR pszConfigFileName;
  152.   DIM AS unsigned long rtnval;
  153.   besARGUMENTS("z[z][z]")
  154.     AT pszProgramFileName, AT pszCmdLineArgs, AT pszConfigFileName
  155.   besARGEND
  156.   pRSP = (struct _RunServiceProgram PTR)malloc( sizeof(struct _RunServiceProgram) );
  157.   pRSP->pszProgramFileName = (char PTR)malloc(strlen(pszProgramFileName) + 1);  
  158.   strcpy(pRSP->pszProgramFileName,pszProgramFileName);
  159.   IF (pszCmdLineArgs NE NULL) THEN
  160.     pRSP->pszCmdLineArgs = (char PTR)malloc(strlen(pszCmdLineArgs) + 1);  
  161.     strcpy(pRSP->pszCmdLineArgs,pszCmdLineArgs);
  162.   ELSE
  163.         pRSP->pszCmdLineArgs = NULL;
  164.   END_IF
  165.   IF (pszConfigFileName NE NULL) THEN
  166.     pRSP->pszConfigFileName = (char PTR)malloc(strlen(pszConfigFileName) + 1);  
  167.     strcpy(pRSP->pszConfigFileName,pszConfigFileName);
  168.   ELSE
  169.         pRSP->pszConfigFileName = NULL;
  170.   END_IF
  171.   pRSP->iRestart = 0;
  172.   thread_CreateThread(AT T,ExecuteProgramThread,pRSP);
  173.   usleep(500);
  174.   rtnval = pRSP->pTProgram;
  175.   besRETURN_LONG(rtnval);
  176. besEND
  177.  
  178. besFUNCTION(SB_ThreadEnd)
  179.   thread_ExitThread();
  180.   besRETURNVALUE = NULL;
  181. besEND
  182.  
  183. besFUNCTION(SB_Destroy)
  184.   DIM AS unsigned long sbobj;
  185.   besARGUMENTS("i")
  186.     AT sbobj
  187.   besARGEND
  188.   scriba_destroy(sbobj);
  189.   RETURN_FUNCTION(0);
  190. besEND
  191.  
  192. besFUNCTION(SB_CallSub)
  193.   DIM AS unsigned long sbobj;
  194.   DIM AS int funcsernum;
  195.   DIM AS char PTR funcname;
  196.   besARGUMENTS("iz")
  197.     AT sbobj, AT funcname
  198.   besARGEND
  199.   funcsernum = scriba_LookupFunctionByName(sbobj, funcname);
  200.   besRETURN_LONG(scriba_Call(sbobj, funcsernum));
  201. besEND
  202.  
  203. besFUNCTION(SB_CallSubArgs)
  204.   DIM AS VARIABLE Argument;
  205.   DIM AS SbData ArgData[8];
  206.   DIM AS SbData FunctionResult;
  207.   DIM AS unsigned long sbobj;
  208.   DIM AS char PTR funcname;
  209.   DIM AS int i, sbtype, fnsn;
  210.  
  211.   Argument = besARGUMENT(1);
  212.   besDEREFERENCE(Argument);
  213.   sbobj = LONGVALUE(Argument);
  214.  
  215.   Argument = besARGUMENT(2);
  216.   besDEREFERENCE(Argument);
  217.   funcname = STRINGVALUE(Argument);
  218.  
  219.   DEF_FOR (i = 3 TO i <= besARGNR STEP INCR i)
  220.   BEGIN_FOR
  221.     Argument = besARGUMENT(i);
  222.     besDEREFERENCE(Argument);
  223.     SELECT_CASE (sbtype = TYPE(Argument))
  224.     BEGIN_SELECT
  225.       CASE VTYPE_LONG:
  226.         ArgData[i-3] = PTR scriba_NewSbLong(sbobj, LONGVALUE(Argument));
  227.         END_CASE
  228.       CASE VTYPE_DOUBLE:
  229.         ArgData[i-3] = PTR scriba_NewSbDouble(sbobj, DOUBLEVALUE(Argument));
  230.         END_CASE
  231.       CASE VTYPE_STRING:
  232.         ArgData[i-3] = PTR scriba_NewSbString(sbobj, STRINGVALUE(Argument));
  233.         END_CASE
  234.       CASE_ELSE
  235.         ArgData[i-3] = PTR scriba_NewSbUndef(sbobj);
  236.         END_CASE
  237.     END_SELECT
  238.   NEXT
  239.  
  240.   fnsn = scriba_LookupFunctionByName(sbobj, funcname);
  241.   scriba_CallArgEx(sbobj, fnsn, AT FunctionResult, besARGNR - 2, AT ArgData);
  242.  
  243.   SELECT_CASE (FunctionResult.type)
  244.   BEGIN_SELECT
  245.     CASE SBT_LONG:
  246.       besRETURN_LONG(FunctionResult.v.l);
  247.       END_CASE
  248.     CASE SBT_DOUBLE:
  249.       besRETURN_DOUBLE(FunctionResult.v.d);
  250.       END_CASE
  251.     CASE SBT_STRING:
  252.       besRETURN_STRING(FunctionResult.v.s);
  253.       END_CASE
  254.     CASE SBT_UNDEF:
  255.       besRETURNVALUE = NULL;
  256.       END_CASE
  257.   END_SELECT
  258. besEND
  259.  
  260. besFUNCTION(SB_GetVar)
  261.   DIM AS pSbData varobj;
  262.   DIM AS unsigned long sbobj;
  263.   DIM AS int vsn;
  264.   DIM AS char PTR varname;
  265.   besARGUMENTS("iz")
  266.     AT sbobj, AT varname
  267.   besARGEND
  268.   vsn = scriba_LookupVariableByName(sbobj, varname);
  269.   scriba_GetVariable(sbobj, vsn, AT varobj);
  270.   SELECT_CASE (scriba_GetVariableType(sbobj, vsn))
  271.   BEGIN_SELECT
  272.     CASE SBT_LONG   :
  273.       besRETURN_LONG(varobj[0].v.l);
  274.       END_CASE
  275.     CASE SBT_DOUBLE :
  276.       besRETURN_DOUBLE(varobj[0].v.d);
  277.       END_CASE
  278.     CASE SBT_STRING :
  279.       besRETURN_STRING(varobj[0].v.s);
  280.       END_CASE
  281.     CASE SBT_UNDEF  :
  282.       besRETURNVALUE = NULL;;
  283.       END_CASE
  284.   END_SELECT
  285. besEND
  286.  
  287. besFUNCTION(SB_SetUndef)
  288.   DIM AS pSbData varobj;
  289.   DIM AS unsigned long sbobj;
  290.   DIM AS int vsn;
  291.   DIM AS char PTR varname;
  292.   besARGUMENTS("iz")
  293.     AT sbobj, AT varname
  294.   besARGEND
  295.   vsn = scriba_LookupVariableByName(sbobj, varname);
  296.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn, SBT_UNDEF, NULL, 0, "", 0));
  297. besEND
  298.  
  299. besFUNCTION(SB_SetInt)
  300.   DIM AS VARIABLE Argument;
  301.   DIM AS pSbData varobj;
  302.   DIM AS unsigned long sbobj;
  303.   DIM AS int vsn, usrval, i;
  304.   DIM AS char PTR varname;
  305.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  306.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  307.   BEGIN_FOR
  308.     Argument = besARGUMENT(i);
  309.     besDEREFERENCE(Argument);
  310.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  311.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  312.     IF (i EQ 3) THEN_DO usrval = LONGVALUE(Argument);
  313.   NEXT
  314.   vsn = scriba_LookupVariableByName(sbobj, varname);
  315.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn, SBT_LONG, usrval, 0, "", 0));
  316. besEND
  317.  
  318. besFUNCTION(SB_SetDbl)
  319.   DIM AS VARIABLE Argument;
  320.   DIM AS pSbData varobj;
  321.   DIM AS unsigned long sbobj;
  322.   DIM AS int vsn, i;
  323.   DIM AS char PTR varname;
  324.   DIM AS double usrval;
  325.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  326.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  327.   BEGIN_FOR
  328.     Argument = besARGUMENT(i);
  329.     besDEREFERENCE(Argument);
  330.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  331.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  332.     IF (i EQ 3) THEN_DO usrval = DOUBLEVALUE(Argument);
  333.   NEXT
  334.   vsn = scriba_LookupVariableByName(sbobj, varname);
  335.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn,  SBT_DOUBLE, 0, usrval, "", 0));
  336. besEND
  337.  
  338. besFUNCTION(SB_SetStr)
  339.   DIM AS VARIABLE Argument;
  340.   DIM AS pSbData varobj;
  341.   DIM AS unsigned long sbobj;
  342.   DIM AS int vsn, i;
  343.   DIM AS char PTR varname;
  344.   DIM AS char PTR usrval;
  345.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  346.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  347.   BEGIN_FOR
  348.     Argument = besARGUMENT(i);
  349.     besDEREFERENCE(Argument);
  350.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  351.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  352.     IF (i EQ 3) THEN_DO usrval = STRINGVALUE(Argument);
  353.   NEXT
  354.   vsn = scriba_LookupVariableByName(sbobj, varname);
  355.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn,  SBT_STRING, 0, 0, usrval, strlen(usrval)));
  356. besEND
  357.  
  358. besFUNCTION(SB_ResetVars)
  359.   DIM AS unsigned long sbobj;
  360.   besARGUMENTS("i")
  361.     AT sbobj
  362.   besARGEND
  363.   scriba_ResetVariables(sbobj);
  364.   besRETURNVALUE = NULL;
  365. besEND
  366.  

sbt.inc
Code: Script BASIC
  1. DECLARE SUB SB_New ALIAS "SB_New" LIB "sbt"
  2. DECLARE SUB SB_Configure ALIAS "SB_Configure" LIB "sbt"
  3. DECLARE SUB SB_Load ALIAS "SB_Load" LIB "sbt"
  4. DECLARE SUB SB_LoadStr ALIAS "SB_LoadStr" LIB "sbt"
  5. DECLARE SUB SB_Run ALIAS "SB_Run" LIB "sbt"
  6. DECLARE SUB SB_NoRun ALIAS "SB_NoRun" LIB "sbt"
  7. DECLARE SUB SB_ThreadStart ALIAS "SB_ThreadStart" LIB "sbt"
  8. DECLARE SUB SB_ThreadEnd ALIAS "SB_ThreadEnd" LIB "sbt"
  9. DECLARE SUB SB_GetVar ALIAS "SB_GetVar" LIB "sbt"
  10. DECLARE SUB SB_SetUndef ALIAS "SB_SetUndef" LIB "sbt"
  11. DECLARE SUB SB_SetInt ALIAS "SB_SetInt" LIB "sbt"
  12. DECLARE SUB SB_SetDbl ALIAS "SB_SetDbl" LIB "sbt"
  13. DECLARE SUB SB_SetStr ALIAS "SB_SetStr" LIB "sbt"
  14. DECLARE SUB SB_ResetVars ALIAS "SB_ResetVars" LIB "sbt"
  15. DECLARE SUB SB_CallSub ALIAS "SB_CallSub" LIB "sbt"
  16. DECLARE SUB SB_CallSubArgs ALIAS "SB_CallSubArgs" LIB "sbt"
  17. DECLARE SUB SB_Destroy ALIAS "SB_Destroy" LIB "sbt"
  18.  

sbtdemo.sb
Code: Script BASIC
  1. ' SBT (Script BASIC Tutorial) - Example Script
  2.  
  3. IMPORT sbt.inc
  4.  
  5. sb_code = """
  6. FUNCTION prtvars(a, b, c)
  7.  PRINT a,"\\n"
  8.  PRINT FORMAT("%g\\n", b)
  9.  PRINT c,"\\n"
  10.  prtvars = "Function Return"
  11. END FUNCTION
  12.  
  13. a = 0
  14. b = 0
  15. c = ""
  16. """
  17.  
  18. sb = SB_New()
  19. SB_Configure sb, "/etc/scriba/basic.conf"
  20. SB_LoadStr sb, sb_code
  21. SB_NoRun sb
  22. funcrtn = SB_CallSubArgs(sb,"main::prtvars", 123, 1.23, "One, Two, Three")
  23. PRINT funcrtn,"\n"
  24. SB_Run sb, ""
  25. SB_SetInt sb, "main::a", 321
  26. SB_SetDbl sb, "main::b", 32.1
  27. SB_SetStr sb, "main::c", "Three,Two,One"
  28. SB_CallSubArgs sb, "main::prtvars", _
  29.           SB_GetVar(sb, "main::a"), _
  30.           SB_GetVar(sb, "main::b"), _
  31.           SB_GetVar(sb, "main::c")      
  32. SB_Destroy sb
  33.  

Output

jrs@laptop:~/sb/sb22/sbt$ time scriba sbtdemo.sb
123
1.23
One, Two, Three
Function Return
321
32.1
Three,Two,One

real   0m0.007s
user   0m0.007s
sys   0m0.000s
jrs@laptop:~/sb/sb22/sbt$


Pages: [1] 2 3 ... 12