Author Topic: Script BASIC Extension Module  (Read 12741 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Script BASIC Extension Module
« on: April 22, 2015, 03:51:11 PM »
I had started on a project to give Script BASIC some of the features Charles Pegge's DLLC offers but on Linux and in 64 bit. I got hung up with my long time nemesis (MyICall) from within an the SB embedding API rather than extension API (which works) and put it on the back burner until Peter or AIR had time to assist. Tony's MY-BASIC project rejuvenated my interest.

interface.c
Code: C
  1. // Script BASIC 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 "../../basext.h"
  10. #include "../../scriba.h"
  11. #include "cbasic.h"
  12.  
  13.  
  14. /****************************
  15.  Extension Module Functions
  16. ****************************/
  17.  
  18. besVERSION_NEGOTIATE
  19.   RETURN_FUNCTION((int)INTERFACE_VERSION);
  20. besEND
  21.  
  22. besSUB_START
  23.   DIM AS long PTR p;
  24.   besMODULEPOINTER = besALLOC(sizeof(long));
  25.   IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
  26.   p = (long PTR)besMODULEPOINTER;
  27.   RETURN_FUNCTION(0);
  28. besEND
  29.  
  30. besSUB_FINISH
  31.   DIM AS long PTR p;
  32.   p = (long PTR)besMODULEPOINTER;
  33.   IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
  34.   RETURN_FUNCTION(0);
  35. besEND
  36.  
  37.  
  38. /**********************
  39.  Script BASIC Instance
  40. **********************/
  41.  
  42. besFUNCTION(SB_New)
  43.   DIM AS pSbProgram pProgram;
  44.   pProgram = scriba_new(malloc,free);
  45.   besRETURN_LONG(pProgram);
  46. besEND
  47.  
  48. besFUNCTION(SB_Configure)
  49.   DIM AS int sbobj;
  50.   DIM AS char PTR cfgfilename;  
  51.   DIM AS int rtnval = -1;
  52.   besARGUMENTS("iz")
  53.     AT sbobj, AT cfgfilename
  54.   besARGEND
  55.   rtnval = scriba_LoadConfiguration(sbobj, cfgfilename);
  56.   besRETURN_LONG(rtnval);
  57. besEND
  58.  
  59. besFUNCTION(SB_Load)  
  60.   DIM AS int sbobj;
  61.   DIM AS char PTR sbfilename;  
  62.   DIM AS int rtnval = -1;
  63.   besARGUMENTS("iz")
  64.     AT sbobj, AT sbfilename
  65.   besARGEND
  66.   rtnval = scriba_SetFileName(sbobj, sbfilename);
  67.   scriba_LoadSourceProgram(sbobj);
  68.   besRETURN_LONG(rtnval);
  69. besEND
  70.  
  71. besFUNCTION(SB_Run)  
  72.   DIM AS int sbobj, rtnval = -1;
  73.   DIM AS char PTR sbcmdline;
  74.   besARGUMENTS("iz")
  75.     AT sbobj, AT sbcmdline
  76.   besARGEND
  77.   IF (besARGNR < 2) THEN_DO sbcmdline = "";
  78.   rtnval = scriba_Run(sbobj, sbcmdline);
  79.   besRETURN_LONG(rtnval);
  80. besEND
  81.  
  82. besFUNCTION(SB_NoRun)  
  83.   DIM AS int sbobj, rtnval = -1;
  84.   besARGUMENTS("i")
  85.     AT sbobj
  86.   besARGEND
  87.   rtnval = scriba_NoRun(sbobj);
  88.   besRETURN_LONG(rtnval);
  89. besEND
  90.  
  91. besFUNCTION(SB_Destroy)
  92.   DIM AS int sbobj;
  93.   besARGUMENTS("i")
  94.     AT sbobj
  95.   besARGEND
  96.   scriba_destroy(sbobj);
  97.   RETURN_FUNCTION(0);
  98. besEND
  99.  
  100. besFUNCTION(SB_Address)
  101.   DIM AS int sbobj, funcsernum;
  102.   DIM AS char PTR funcname;
  103.   besARGUMENTS("iz")
  104.     AT sbobj, AT funcname
  105.   besARGEND
  106.   funcsernum = scriba_LookupFunctionByName(sbobj, funcname);
  107.   besRETURN_LONG(funcsernum);
  108. besEND
  109.  
  110. // besFUNCTION(SB_Global)
  111. //   DIM AS pSbData varobj;
  112. //   DIM AS int sbobj, vsn;
  113. //   DIM AS char PTR varname;
  114. //   besARGUMENTS("iz")
  115. //     AT sbobj, AT varname
  116. //   besARGEND
  117. //   vsn = scriba_LookupVariableByName(sbobj, varname);
  118. //   scriba_GetVariable(sbobj, vsn, &varobj);
  119. //   switch(varobj[0].type){
  120. //     case VTYPE_LONG:
  121. //       besRETURN_LONG(varobj[0].v.l);
  122. //       break;
  123. //     case VTYPE_DOUBLE:
  124. //       besRETURN_DOUBLE(varobj[0].v.d);
  125. //       break;
  126. //     case VTYPE_STRING:
  127. //       besRETURN_STRING(varobj[0].v.s);
  128. //       break;
  129. //     }
  130. //   }
  131. // besEND
  132.  

embed.sb
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_Run ALIAS "SB_Run" LIB "sbt"
  5. DECLARE SUB SB_NoRun ALIAS "SB_NoRun" LIB "sbt"
  6. DECLARE SUB SB_Destroy ALIAS "SB_Destroy" LIB "sbt"
  7.  
  8. sb = SB_New()
  9. ok = SB_Configure(sb, "/etc/scriba/basic.conf")
  10. ok = SB_Load(sb, "test.sb")
  11. ok = SB_Run(sb, "Junk_In_The_Trunk")
  12. SB_Destroy(sb)
  13.  

test.sb
Code: Script BASIC
  1. cmd = COMMAND()
  2.  
  3. PRINT "ARG = ",cmd,"\n"
  4.  
  5. FOR x = 1 TO 5
  6.   PRINT x,"\n"
  7. NEXT
  8.  

Output

jrs@laptop:~/sb/sb22/sbt$ time scriba embed.sb
ARG = Junk_In_The_Trunk
1
2
3
4
5

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


 
« Last Edit: April 22, 2015, 03:53:39 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #1 on: April 22, 2015, 05:07:31 PM »
I added support for getting variables from the embedded SB script. I'm working on returning an SB array currently and then I'll tackle the SB_SetXxx functions. Next will be calling SUB/FUNCTION's in the embedded SB script. Lastly will be creating threaded instances of SB.

FYI: The above functionality (and much more) has been working for some time on Script BASIC for Windows using Charles Pegge's DLLC8)

FYI2: The above functionality already works multi-platform in the form of sbhttpd. (multi-threaded http proxy server that runs as a service using C as the host language)

interface.c
Code: C
  1. // Script BASIC 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 "../../basext.h"
  10. #include "../../scriba.h"
  11. #include "cbasic.h"
  12.  
  13.  
  14. /****************************
  15.  Extension Module Functions
  16. ****************************/
  17.  
  18. besVERSION_NEGOTIATE
  19.   RETURN_FUNCTION((int)INTERFACE_VERSION);
  20. besEND
  21.  
  22. besSUB_START
  23.   DIM AS long PTR p;
  24.   besMODULEPOINTER = besALLOC(sizeof(long));
  25.   IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
  26.   p = (long PTR)besMODULEPOINTER;
  27.   RETURN_FUNCTION(0);
  28. besEND
  29.  
  30. besSUB_FINISH
  31.   DIM AS long PTR p;
  32.   p = (long PTR)besMODULEPOINTER;
  33.   IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
  34.   RETURN_FUNCTION(0);
  35. besEND
  36.  
  37.  
  38. /**********************
  39.  Script BASIC Instance
  40. **********************/
  41.  
  42. besFUNCTION(SB_New)
  43.   DIM AS pSbProgram pProgram;
  44.   pProgram = scriba_new(malloc,free);
  45.   besRETURN_LONG(pProgram);
  46. besEND
  47.  
  48. besFUNCTION(SB_Configure)
  49.   DIM AS int sbobj;
  50.   DIM AS char PTR cfgfilename;  
  51.   DIM AS int rtnval = -1;
  52.   besARGUMENTS("iz")
  53.     AT sbobj, AT cfgfilename
  54.   besARGEND
  55.   rtnval = scriba_LoadConfiguration(sbobj, cfgfilename);
  56.   besRETURN_LONG(rtnval);
  57. besEND
  58.  
  59. besFUNCTION(SB_Load)  
  60.   DIM AS int sbobj;
  61.   DIM AS char PTR sbfilename;  
  62.   DIM AS int rtnval = -1;
  63.   besARGUMENTS("iz")
  64.     AT sbobj, AT sbfilename
  65.   besARGEND
  66.   rtnval = scriba_SetFileName(sbobj, sbfilename);
  67.   scriba_LoadSourceProgram(sbobj);
  68.   besRETURN_LONG(rtnval);
  69. besEND
  70.  
  71. besFUNCTION(SB_Run)  
  72.   DIM AS int sbobj, rtnval = -1;
  73.   DIM AS char PTR sbcmdline;
  74.   besARGUMENTS("iz")
  75.     AT sbobj, AT sbcmdline
  76.   besARGEND
  77.   IF (besARGNR < 2) THEN_DO sbcmdline = "";
  78.   rtnval = scriba_Run(sbobj, sbcmdline);
  79.   besRETURN_LONG(rtnval);
  80. besEND
  81.  
  82. besFUNCTION(SB_NoRun)  
  83.   DIM AS int sbobj, rtnval = -1;
  84.   besARGUMENTS("i")
  85.     AT sbobj
  86.   besARGEND
  87.   rtnval = scriba_NoRun(sbobj);
  88.   besRETURN_LONG(rtnval);
  89. besEND
  90.  
  91. besFUNCTION(SB_Destroy)
  92.   DIM AS int sbobj;
  93.   besARGUMENTS("i")
  94.     AT sbobj
  95.   besARGEND
  96.   scriba_destroy(sbobj);
  97.   RETURN_FUNCTION(0);
  98. besEND
  99.  
  100. besFUNCTION(SB_Address)
  101.   DIM AS int sbobj, funcsernum;
  102.   DIM AS char PTR funcname;
  103.   besARGUMENTS("iz")
  104.     AT sbobj, AT funcname
  105.   besARGEND
  106.   funcsernum = scriba_LookupFunctionByName(sbobj, funcname);
  107.   besRETURN_LONG(funcsernum);
  108. besEND
  109.  
  110. besFUNCTION(SB_GetInt)
  111.   DIM AS pSbData varobj;                            
  112.   DIM AS int sbobj, vsn;                            
  113.   DIM AS char PTR varname;                          
  114.   besARGUMENTS("iz")                                
  115.     AT sbobj, AT varname                            
  116.   besARGEND                                        
  117.   vsn = scriba_LookupVariableByName(sbobj, varname);
  118.   scriba_GetVariable(sbobj, vsn, &varobj);          
  119.   besRETURN_LONG(varobj[0].v.l);
  120. besEND
  121.  
  122. besFUNCTION(SB_GetDbl)
  123.   DIM AS pSbData varobj;                            
  124.   DIM AS int sbobj, vsn;                            
  125.   DIM AS char PTR varname;                          
  126.   besARGUMENTS("iz")                                
  127.     AT sbobj, AT varname                            
  128.   besARGEND                                        
  129.   vsn = scriba_LookupVariableByName(sbobj, varname);
  130.   scriba_GetVariable(sbobj, vsn, &varobj);          
  131.   besRETURN_DOUBLE(varobj[0].v.d);
  132. besEND
  133.  
  134. besFUNCTION(SB_GetStr)
  135.   DIM AS pSbData varobj;                            
  136.   DIM AS int sbobj, vsn;                            
  137.   DIM AS char PTR varname;                          
  138.   besARGUMENTS("iz")                                
  139.     AT sbobj, AT varname                            
  140.   besARGEND                                        
  141.   vsn = scriba_LookupVariableByName(sbobj, varname);
  142.   scriba_GetVariable(sbobj, vsn, &varobj);          
  143.   besRETURN_STRING(varobj[0].v.s);
  144. besEND
  145.  

embed.sb
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_Run ALIAS "SB_Run" LIB "sbt"
  5. DECLARE SUB SB_NoRun ALIAS "SB_NoRun" LIB "sbt"
  6. DECLARE SUB SB_GetInt ALIAS "SB_GetInt" LIB "sbt"
  7. DECLARE SUB SB_GetDbl ALIAS "SB_GetDbl" LIB "sbt"
  8. DECLARE SUB SB_GetStr ALIAS "SB_GetStr" LIB "sbt"
  9. DECLARE SUB SB_Destroy ALIAS "SB_Destroy" LIB "sbt"
  10.  
  11. sb = SB_New()
  12. SB_Configure(sb, "/etc/scriba/basic.conf")
  13. SB_Load(sb, "testvars.sb")
  14. SB_Run(sb,"")
  15. PRINT SB_GetInt(sb, "main::a"),"\n"
  16. PRINT FORMAT("%g\n",SB_GetDbl(sb, "main::b"))
  17. PRINT SB_GetStr(sb, "main::c"),"\n"
  18. SB_Destroy(sb)
  19.  

testvars.sb
Code: Script BASIC
  1. a = 123
  2. b = 1.23
  3. c = "One,Two,Three"
  4.  

Output

jrs@laptop:~/sb/sb22/sbt$ scriba embed.sb
123
1.23
One,Two,Three
jrs@laptop:~/sb/sb22/sbt$
« Last Edit: April 22, 2015, 07:05:52 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #2 on: April 22, 2015, 08:18:16 PM »
I added the ability to load the embedded script as a string as well as a file.

interface.c (change from last full source post)
Code: C
  1. besFUNCTION(SB_LoadStr)  
  2.   DIM AS int sbobj;
  3.   DIM AS char PTR sbpgm;  
  4.   DIM AS int rtnval = -1;
  5.   besARGUMENTS("iz")
  6.     AT sbobj, AT sbpgm
  7.   besARGEND
  8.   scriba_SetFileName(sbobj, "fake");
  9.   rtnval = scriba_LoadProgramString(sbobj, sbpgm, strlen(sbpgm));
  10.   besRETURN_LONG(rtnval);
  11. besEND
  12.  

embedprime.sb
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_GetInt ALIAS "SB_GetInt" LIB "sbt"
  8. DECLARE SUB SB_GetDbl ALIAS "SB_GetDbl" LIB "sbt"
  9. DECLARE SUB SB_GetStr ALIAS "SB_GetStr" LIB "sbt"
  10. DECLARE SUB SB_Destroy ALIAS "SB_Destroy" LIB "sbt"
  11.  
  12. sb = SB_New()
  13. SB_Configure(sb, "/etc/scriba/basic.conf")
  14. sb_code = """
  15. FOR i = 3 TO 5000
  16.  FOR j = 2 TO SQR(i)
  17.    IF i % j = 0 THEN GOTO nDone
  18.  NEXT
  19.  PRINT i," "
  20.  nDone:
  21. NEXT
  22. PRINTNL
  23. """
  24. SB_LoadStr(sb, sb_code)
  25. SB_Run(sb,"")
  26. SB_Destroy(sb)
  27.  

Output

jrs@laptop:~/sb/sb22/sbt$ time scriba embedprime.sb
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423 2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939 2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 3581 3583 3593 3607 3613 3617 3623 3631 3637 3643 3659 3671 3673 3677 3691 3697 3701 3709 3719 3727 3733 3739 3761 3767 3769 3779 3793 3797 3803 3821 3823 3833 3847 3851 3853 3863 3877 3881 3889 3907 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 4001 4003 4007 4013 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 4099 4111 4127 4129 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 4241 4243 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349 4357 4363 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493 4507 4513 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621 4637 4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729 4733 4751 4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871 4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973 4987 4993 4999

real   0m0.039s
user   0m0.034s
sys   0m0.004s
jrs@laptop:~/sb/sb22/sbt$

Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #3 on: April 24, 2015, 11:50:14 PM »
I have added the SB_Setxxx functions to the extension module.

interface.c
Code: C
  1. // Script BASIC 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 "../../basext.h"
  10. #include "../../scriba.h"
  11. #include "cbasic.h"
  12.  
  13.  
  14. /****************************
  15.  Extension Module Functions
  16. ****************************/
  17.  
  18. besVERSION_NEGOTIATE
  19.   RETURN_FUNCTION((int)INTERFACE_VERSION);
  20. besEND
  21.  
  22. besSUB_START
  23.   DIM AS long PTR p;
  24.   besMODULEPOINTER = besALLOC(sizeof(long));
  25.   IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
  26.   p = (long PTR)besMODULEPOINTER;
  27.   RETURN_FUNCTION(0);
  28. besEND
  29.  
  30. besSUB_FINISH
  31.   DIM AS long PTR p;
  32.   p = (long PTR)besMODULEPOINTER;
  33.   IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
  34.   RETURN_FUNCTION(0);
  35. besEND
  36.  
  37.  
  38. /**********************
  39.  Script BASIC Instance
  40. **********************/
  41.  
  42. besFUNCTION(SB_New)
  43.   DIM AS pSbProgram pProgram;
  44.   pProgram = scriba_new(malloc,free);
  45.   besRETURN_LONG(pProgram);
  46. besEND
  47.  
  48. besFUNCTION(SB_Configure)
  49.   DIM AS int sbobj;
  50.   DIM AS char PTR cfgfilename;  
  51.   DIM AS int rtnval = -1;
  52.   besARGUMENTS("iz")
  53.     AT sbobj, AT cfgfilename
  54.   besARGEND
  55.   rtnval = scriba_LoadConfiguration(sbobj, cfgfilename);
  56.   besRETURN_LONG(rtnval);
  57. besEND
  58.  
  59. besFUNCTION(SB_Load)  
  60.   DIM AS int sbobj;
  61.   DIM AS char PTR sbfilename;  
  62.   DIM AS int rtnval = -1;
  63.   besARGUMENTS("iz")
  64.     AT sbobj, AT sbfilename
  65.   besARGEND
  66.   rtnval = scriba_SetFileName(sbobj, sbfilename);
  67.   scriba_LoadSourceProgram(sbobj);
  68.   besRETURN_LONG(rtnval);
  69. besEND
  70.  
  71. besFUNCTION(SB_LoadStr)  
  72.   DIM AS int sbobj;
  73.   DIM AS char PTR sbpgm;  
  74.   DIM AS int rtnval = -1;
  75.   besARGUMENTS("iz")
  76.     AT sbobj, AT sbpgm
  77.   besARGEND
  78.   scriba_SetFileName(sbobj, "fake");
  79.   rtnval = scriba_LoadProgramString(sbobj, sbpgm, strlen(sbpgm));
  80.   besRETURN_LONG(rtnval);
  81. besEND
  82.  
  83. besFUNCTION(SB_Run)  
  84.   DIM AS int sbobj, rtnval = -1;
  85.   DIM AS char PTR sbcmdline;
  86.   besARGUMENTS("iz")
  87.     AT sbobj, AT sbcmdline
  88.   besARGEND
  89.   IF (besARGNR < 2) THEN_DO sbcmdline = "";
  90.   rtnval = scriba_Run(sbobj, sbcmdline);
  91.   besRETURN_LONG(rtnval);
  92. besEND
  93.  
  94. besFUNCTION(SB_NoRun)  
  95.   DIM AS int sbobj, rtnval = -1;
  96.   besARGUMENTS("i")
  97.     AT sbobj
  98.   besARGEND
  99.   rtnval = scriba_NoRun(sbobj);
  100.   besRETURN_LONG(rtnval);
  101. besEND
  102.  
  103. besFUNCTION(SB_Destroy)
  104.   DIM AS int sbobj;
  105.   besARGUMENTS("i")
  106.     AT sbobj
  107.   besARGEND
  108.   scriba_destroy(sbobj);
  109.   RETURN_FUNCTION(0);
  110. besEND
  111.  
  112. besFUNCTION(SB_Address)
  113.   DIM AS int sbobj, funcsernum;
  114.   DIM AS char PTR funcname;
  115.   besARGUMENTS("iz")
  116.     AT sbobj, AT funcname
  117.   besARGEND
  118.   funcsernum = scriba_LookupFunctionByName(sbobj, funcname);
  119.   besRETURN_LONG(funcsernum);
  120. besEND
  121.  
  122. besFUNCTION(SB_GetInt)
  123.   DIM AS pSbData varobj;                            
  124.   DIM AS int sbobj, vsn;                            
  125.   DIM AS char PTR varname;                          
  126.   besARGUMENTS("iz")                                
  127.     AT sbobj, AT varname                            
  128.   besARGEND                                        
  129.   vsn = scriba_LookupVariableByName(sbobj, varname);
  130.   scriba_GetVariable(sbobj, vsn, &varobj);          
  131.   besRETURN_LONG(varobj[0].v.l);
  132. besEND
  133.  
  134. besFUNCTION(SB_GetDbl)
  135.   DIM AS pSbData varobj;                            
  136.   DIM AS int sbobj, vsn;                            
  137.   DIM AS char PTR varname;                          
  138.   besARGUMENTS("iz")                                
  139.     AT sbobj, AT varname                            
  140.   besARGEND                                        
  141.   vsn = scriba_LookupVariableByName(sbobj, varname);
  142.   scriba_GetVariable(sbobj, vsn, &varobj);          
  143.   besRETURN_DOUBLE(varobj[0].v.d);
  144. besEND
  145.  
  146. besFUNCTION(SB_GetStr)
  147.   DIM AS pSbData varobj;                            
  148.   DIM AS int sbobj, vsn;                            
  149.   DIM AS char PTR varname;                          
  150.   besARGUMENTS("iz")                                
  151.     AT sbobj, AT varname                            
  152.   besARGEND                                        
  153.   vsn = scriba_LookupVariableByName(sbobj, varname);
  154.   scriba_GetVariable(sbobj, vsn, &varobj);          
  155.   besRETURN_STRING(varobj[0].v.s);
  156. besEND
  157.  
  158. besFUNCTION(SB_SetInt)
  159.   DIM AS VARIABLE Argument;
  160.   DIM AS pSbData varobj;                            
  161.   DIM AS int sbobj, vsn, usrval, i;                            
  162.   DIM AS char PTR varname;                          
  163.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  164.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  165.   BEGIN_FOR
  166.     Argument = besARGUMENT(i);
  167.     besDEREFERENCE(Argument);
  168.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  169.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  170.     IF (i EQ 3) THEN_DO usrval = LONGVALUE(Argument);
  171.   NEXT
  172.   vsn = scriba_LookupVariableByName(sbobj, varname);
  173.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn, SBT_LONG, usrval, 0, "", 0));
  174. besEND
  175.  
  176. besFUNCTION(SB_SetDbl)
  177.   DIM AS VARIABLE Argument;
  178.   DIM AS pSbData varobj;                            
  179.   DIM AS int sbobj, vsn, i;                            
  180.   DIM AS char PTR varname;                          
  181.   DIM AS double usrval;
  182.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  183.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  184.   BEGIN_FOR
  185.     Argument = besARGUMENT(i);
  186.     besDEREFERENCE(Argument);
  187.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  188.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  189.     IF (i EQ 3) THEN_DO usrval = DOUBLEVALUE(Argument);
  190.   NEXT
  191.   vsn = scriba_LookupVariableByName(sbobj, varname);
  192.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn,  SBT_DOUBLE, 0, usrval, "", 0));
  193. besEND
  194.  
  195. besFUNCTION(SB_SetStr)
  196.   DIM AS VARIABLE Argument;
  197.   DIM AS pSbData varobj;                            
  198.   DIM AS int sbobj, vsn, i;                            
  199.   DIM AS char PTR varname;                          
  200.   DIM AS char PTR usrval;
  201.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  202.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  203.   BEGIN_FOR
  204.     Argument = besARGUMENT(i);
  205.     besDEREFERENCE(Argument);
  206.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  207.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  208.     IF (i EQ 3) THEN_DO usrval = STRINGVALUE(Argument);
  209.   NEXT
  210.   vsn = scriba_LookupVariableByName(sbobj, varname);
  211.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn,  SBT_STRING, 0, 0, usrval, strlen(usrval)));
  212. besEND
  213.  
  214. besFUNCTION(SB_ResetVars)
  215.   DIM AS int sbobj;
  216.   besARGUMENTS("i")
  217.     AT sbobj
  218.   besARGEND
  219.   scriba_ResetVariables(sbobj);
  220.   besRETURNVALUE = NULL;
  221. besEND
  222.  

testvars.sb
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_Run ALIAS "SB_Run" LIB "sbt"
  5. DECLARE SUB SB_NoRun ALIAS "SB_NoRun" LIB "sbt"
  6. DECLARE SUB SB_GetInt ALIAS "SB_GetInt" LIB "sbt"
  7. DECLARE SUB SB_GetDbl ALIAS "SB_GetDbl" LIB "sbt"
  8. DECLARE SUB SB_GetStr ALIAS "SB_GetStr" LIB "sbt"
  9. DECLARE SUB SB_SetInt ALIAS "SB_SetInt" LIB "sbt"
  10. DECLARE SUB SB_SetDbl ALIAS "SB_SetDbl" LIB "sbt"
  11. DECLARE SUB SB_SetStr ALIAS "SB_SetStr" LIB "sbt"
  12. DECLARE SUB SB_Destroy ALIAS "SB_Destroy" LIB "sbt"
  13.  
  14. sb = SB_New()
  15. SB_Configure sb, "/etc/scriba/basic.conf"
  16. SB_Load sb, "setvars.sb"
  17. SB_Run sb, ""
  18. SB_SetInt sb, "main::a", 123
  19. SB_SetDbl sb, "main::b", 1.23
  20. SB_SetStr sb, "main::c", "One,Two,Three"
  21. PRINT SB_GetInt(sb, "main::a"),"\n"
  22. PRINT FORMAT("%g\n",SB_GetDbl(sb, "main::b"))
  23. PRINT SB_GetStr(sb, "main::c"),"\n"
  24. SB_Destroy sb
  25.  

setvars.sb
Code: Script BASIC
  1. a = 0
  2. b = 0.0
  3. c = ""
  4.  

Output (Linux 64 bit)

jrs@laptop:~/sb/sb22/sbt$ time scriba testvars.sb
123
1.23
One,Two,Three

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


Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #4 on: April 25, 2015, 09:44:55 AM »
Here is an example of calling a Script BASIC embedded script SUB. (no arguments being passed)

Code: C
  1. besFUNCTION(SB_CallSub)
  2.   DIM AS int sbobj, funcsernum;
  3.   DIM AS char PTR funcname;
  4.   besARGUMENTS("iz")
  5.     AT sbobj, AT funcname
  6.   besARGEND
  7.   funcsernum = scriba_LookupFunctionByName(sbobj, funcname);
  8.   besRETURN_LONG(scriba_Call(sbobj, funcsernum));
  9. besEND
  10.  

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_Run ALIAS "SB_Run" LIB "sbt"
  5. DECLARE SUB SB_NoRun ALIAS "SB_NoRun" LIB "sbt"
  6. DECLARE SUB SB_GetInt ALIAS "SB_GetInt" LIB "sbt"
  7. DECLARE SUB SB_GetDbl ALIAS "SB_GetDbl" LIB "sbt"
  8. DECLARE SUB SB_GetStr ALIAS "SB_GetStr" LIB "sbt"
  9. DECLARE SUB SB_SetInt ALIAS "SB_SetInt" LIB "sbt"
  10. DECLARE SUB SB_SetDbl ALIAS "SB_SetDbl" LIB "sbt"
  11. DECLARE SUB SB_SetStr ALIAS "SB_SetStr" LIB "sbt"
  12. DECLARE SUB SB_CallSub ALIAS "SB_CallSub" LIB "sbt"
  13. DECLARE SUB SB_Destroy ALIAS "SB_Destroy" LIB "sbt"
  14.  
  15. sb = SB_New()
  16. SB_Configure sb, "/etc/scriba/basic.conf"
  17. SB_Load sb, "embedsub.sb"
  18. SB_NoRun sb
  19. SB_CallSub sb, "main::setvars"
  20. PRINT SB_GetInt(sb, "main::a"),"\n"
  21. PRINT FORMAT("%g\n",SB_GetDbl(sb, "main::b"))
  22. PRINT SB_GetStr(sb, "main::c"),"\n"
  23. SB_Destroy sb
  24.  

embedsub.sb
Code: Script BASIC
  1. SUB setvars
  2.   a = 123
  3.   b = 1.23
  4.   c = "One,Two,Three"
  5. END SUB
  6.  


jrs@laptop:~/sb/sb22/sbt$ time scriba testsub.sb
123
1.23
One,Two,Three

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


Passing arguments and returning a FUNCTION result is my never ending battle. I have asked Peter Verhas, Charles Pegge and AIR for help with this in the past and remains on the table. It would be great if we can all put our heads together and resolve this once and for all. Charles has resolved this in DLLC and Dave in his COM adventures. I hope they read this and share their knowledge.
« Last Edit: April 25, 2015, 09:46:32 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #5 on: April 25, 2015, 11:48:02 AM »
In essence I want to do what ICALL is doing from my HOST SB script.

testicall.sb
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_Run ALIAS "SB_Run" LIB "sbt"
  5. DECLARE SUB SB_NoRun ALIAS "SB_NoRun" LIB "sbt"
  6. DECLARE SUB SB_GetInt ALIAS "SB_GetInt" LIB "sbt"
  7. DECLARE SUB SB_GetDbl ALIAS "SB_GetDbl" LIB "sbt"
  8. DECLARE SUB SB_GetStr ALIAS "SB_GetStr" LIB "sbt"
  9. DECLARE SUB SB_SetInt ALIAS "SB_SetInt" LIB "sbt"
  10. DECLARE SUB SB_SetDbl ALIAS "SB_SetDbl" LIB "sbt"
  11. DECLARE SUB SB_SetStr ALIAS "SB_SetStr" LIB "sbt"
  12. DECLARE SUB SB_CallSub ALIAS "SB_CallSub" LIB "sbt"
  13. DECLARE SUB SB_Destroy ALIAS "SB_Destroy" LIB "sbt"
  14.  
  15. sb = SB_New()
  16. SB_Configure sb, "/etc/scriba/basic.conf"
  17. SB_Load sb, "embedicall.sb"
  18. SB_Run sb, ""
  19. PRINT SB_GetInt(sb, "main::fn_a"),"\n"
  20. PRINT FORMAT("%g\n",SB_GetDbl(sb, "main::fn_b"))
  21. PRINT SB_GetStr(sb, "main::fn_c"),"\n"
  22. SB_Destroy sb
  23.  

embedicall.sb
Code: Script BASIC
  1. FUNCTION setvars(a,b,c)
  2.   fn_a = a
  3.   fn_b = b
  4.   fn_c = c
  5.   setvars = "Thank You!"
  6. END FUNCTION
  7.  
  8. faddr = ADDRESS(setvars())
  9. favor = ICALL(faddr, 123, 1.23, "One,Two,Three")
  10. PRINT favor,"\n"
  11.  

Output

jrs@laptop:~/sb/sb22/sbt$ time scriba testicall.sb
Thank You!
123
1.23
One,Two,Three

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



Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #6 on: April 25, 2015, 11:16:16 PM »
This is a summary of the goals to get this working. This is the host function call I would like to make.

Code: Script BASIC
  1. script_function_return_value = SB_CallFunction(sb, "main::setvars", 123, 1.23, "One,Two,Three")
  2.  

Code: C
  1. SCRIBA_MAIN_LIBSPEC int scriba_CallArgEx(pSbProgram pProgram,
  2.                      unsigned long lEntryNode,
  3.                      pSbData ReturnValue,
  4.                      unsigned long cArgs,
  5.                      pSbData Args);
  6.  

Sure would be nice to be able to call this function dynamically.

Code: C
  1. MyArgs = scriba_NewSbArgs(sbobj,"i r s",123,1.23,"One,Two,Three");
  2.  

The main goal is to dynamically create the Args array for this function based on the arguments being passed in the wrapper extension module call. Ideally it would be nice to use the Args array from the calling function (- the first two arguments which is sbobj and the SB script function name) and use it for the scriba_CallArgEx Args array.

Return SbData structure for the ReturnValue back to the host script. Normally I have to use one of the SB return maco based on type. The return type could be a LONG, DOUBLE or STRING and is based on the function being called.

Quote from: rosettacode
The ANSI C standard header stdarg.h defines macros for low-level access to the parameter stack. It does not know the number or types of these parameters; this is specified by the required initial parameter(s). For example, it could be a simple count, a terminating NULL, or a more complicated parameter specification like a printf() format string.

Code: C
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3.  
  4. void varstrings(int count, ...)   /* the ellipsis indicates variable arguments */
  5. {
  6.     va_list args;
  7.     va_start(args, count);
  8.     while (count--)
  9.         puts(va_arg(args, const char *));
  10.     va_end(args);
  11. }
  12.  
  13. varstrings(5, "Mary", "had", "a", "little", "lamb");
  14.  

In C, there is no way to call a variadic function on a list of arguments constructed at runtime.

However, all standard library functions which are variadic have a corresponding version, usually named by prepending the letter "v", that is non-variadic and takes a va_list as argument in place of the variadic arguments. For example, printf has a corresponding vprintf which takes a format string and a va_list value as arguments.

Nevertheless, the only way of obtaining a va_list is from a variadic function itself. So the "v" functions are only useful for writing a variadic function "wrapper" that performs some processing and then calls on one of the "v" functions with its va_list. C still provides no standard way to construct a va_list manually at runtime.

The actual implementation of va_list is implementation-dependent. If you are developing on a specific platform, you may use platform-specific knowledge to create a va_list by hand in a non-portable way. For example, on many platforms, a va_list is simply a pointer to a buffer where the arguments are arranged contiguously in memory.

I found an example of calling scriba_CallArgEx in DLLC which I think is getting us closer.

Code: OxygenBasic
  1.   function StartTranThread(sys*dat) external
  2.   ==========================================
  3.   SupportTable t at (dat[1])
  4.   SbData ArgData[2],ReturnData
  5.   ArgData[1].typ=SBT_LONG 'DOUBLE 'STRING 'ZCHAR
  6.  ArgData[1].gen=dat[5] 'pprog
  7.  ArgData[2].typ=SBT_LONG
  8.   ArgData[2].gen=dat[6] 'idat
  9.  '
  10.  if dat[3]
  11.     t.scriba_CallArgEx(dat[2],dat[3],@ReturnData,2,@ArgData)
  12.     j=ReturnData.lng
  13.     if j>0
  14.       r=ll[j].handle
  15.       call r 'eg IupLoop
  16.    end if
  17.   end if
  18.   if dat[4]
  19.     t.scriba_CallArgEx(dat[2],dat[4],@ReturnData,2,@ArgData)
  20.   end if
  21.   '
  22.  t.scriba_destroy dat[2] 'pProgram
  23.  freememory @dat 'release param block
  24.  end function
  25.  

Here is the SBT makefile for Linux 64 bit I'm using.

Code: GNU make
  1.  
  2.  
  3.  
  4. all : /home/jrs/sb/source/bin/mod/lib/sbt.a /home/jrs/sb/source/bin/mod/dll/sbt.so
  5.  
  6.  
  7.  
  8.  
  9. /home/jrs/sb/source/bin/mod/lib/sbt.a : /home/jrs/sb/source/bin/mod/obj/sbt/s_interface.o
  10.         ar -r /home/jrs/sb/source/bin/mod/lib/sbt.a /home/jrs/sb/source/bin/mod/obj/sbt/s_interface.o  
  11.  
  12. /home/jrs/sb/source/bin/mod/dll/sbt.so : /home/jrs/sb/source/bin/mod/obj/sbt/interface.o
  13.         ld -shared -fPIC -o /home/jrs/sb/source/bin/mod/dll/sbt.so /home/jrs/sb/source/bin/mod/obj/sbt/interface.o -lscriba -lpthread -lm
  14.  
  15. /home/jrs/sb/source/bin/mod/obj/sbt/interface.o : interface.c
  16.         gcc -O2 -w -m64 -fPIC -c -o /home/jrs/sb/source/bin/mod/obj/sbt/interface.o interface.c
  17.  
  18. /home/jrs/sb/source/bin/mod/obj/sbt/s_interface.o : interface.c
  19.         gcc -O2 -w -m64 -fPIC -DSTATIC_LINK=1 -c -o /home/jrs/sb/source/bin/mod/obj/sbt/s_interface.o interface.c
  20.  
« Last Edit: April 27, 2015, 03:44:30 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #7 on: April 27, 2015, 09:08:08 PM »
I tried to use the MyICall from the trial.so example but no luck. I think I'm close though. At lease it compiles without errors.

The problem I'm dealing with is the MyICall example Peter Verhas did in trial.so is using the besHOOK_CALLSCRIBAFUNCTION macro (masking pSbProgram) to use the embedded API scriba_CallArgEx in a extension API environment. The scriba_Call function works fine so this is an argument passing and a dynamic script function value return issue only. I think I'm close with the last MyICall translation.

Code: C
  1. besFUNCTION(SB_Address)
  2.   DIM AS int sbobj, funcsernum;
  3.   DIM AS char PTR funcname;
  4.   besARGUMENTS("iz")
  5.     AT sbobj, AT funcname
  6.   besARGEND
  7.   funcsernum = scriba_LookupFunctionByName(sbobj, funcname);
  8.   besRETURN_LONG(funcsernum);
  9. besEND
  10.  
  11. besFUNCTION(SB_CallSubArgs)
  12.   VARIABLE Argument;
  13.   VARIABLE pArgument;
  14.   VARIABLE FunctionResult;
  15.   int sbobj;
  16.   unsigned long ulEntryPoint;
  17.   unsigned long i;
  18.  
  19.   Argument = besARGUMENT(1);
  20.   besDEREFERENCE(Argument);
  21.   sbobj = LONGVALUE(Argument);
  22.  
  23.   Argument = besARGUMENT(2);
  24.   besDEREFERENCE(Argument);
  25.   ulEntryPoint = LONGVALUE(Argument);
  26.  
  27.   pArgument = besNEWARRAY(0,besARGNR-3);
  28.   for( i=3 ; i <= (unsigned)besARGNR ; i++ ){
  29.      pArgument->Value.aValue[i-3] = besARGUMENT(i);
  30.      }
  31.   scriba_CallArgEx(sbobj, ulEntryPoint, &FunctionResult, besARGNR-2, pArgument->Value.aValue);
  32.  
  33.   for( i=3 ; i <= (unsigned)besARGNR ; i++ ){
  34.      pArgument->Value.aValue[i-3] = NULL;
  35.      }
  36.   besRELEASE(pArgument);
  37.   besRELEASE(FunctionResult);
  38. besEND
  39.  

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_Run ALIAS "SB_Run" LIB "sbt"
  5. DECLARE SUB SB_NoRun ALIAS "SB_NoRun" LIB "sbt"
  6. DECLARE SUB SB_GetInt ALIAS "SB_GetInt" LIB "sbt"
  7. DECLARE SUB SB_GetDbl ALIAS "SB_GetDbl" LIB "sbt"
  8. DECLARE SUB SB_GetStr ALIAS "SB_GetStr" LIB "sbt"
  9. DECLARE SUB SB_SetInt ALIAS "SB_SetInt" LIB "sbt"
  10. DECLARE SUB SB_SetDbl ALIAS "SB_SetDbl" LIB "sbt"
  11. DECLARE SUB SB_SetStr ALIAS "SB_SetStr" LIB "sbt"
  12. DECLARE SUB SB_Address ALIAS "SB_Address" LIB "sbt"
  13. DECLARE SUB SB_CallSub ALIAS "SB_CallSub" LIB "sbt"
  14. DECLARE SUB SB_CallSubArgs ALIAS "SB_CallSubArgs" LIB "sbt"
  15. DECLARE SUB SB_Destroy ALIAS "SB_Destroy" LIB "sbt"
  16.  
  17. sb = SB_New()
  18. SB_Configure sb, "/etc/scriba/basic.conf"
  19. SB_Load sb, "prtvars.sb"
  20. SB_NoRun sb
  21. fnsn = SB_Address(sb, "main::prtvars")
  22. SB_CallSubArgs(sb, fnsn, 123, 1.23, "One,Two,Three")
  23. SB_Destroy sb
  24.  

Code: Script BASIC
  1. SUB prtvars(a, b, c)
  2.   PRINT a,"\n"
  3.   PRINT FORMAT("%g\n", b)
  4.   PRINT c,"\n"
  5. END SUB
  6.  
« Last Edit: April 28, 2015, 09:47:51 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #8 on: April 28, 2015, 06:20:55 PM »
Here are a few comments from Charles Pegge on the OxygenBasic forum.

Quote from: Charles Pegge
Are you using Hook functions? Last time I attempted to use them, I found null pointers in the chain. ie: structures not initialised.

However, using the scriba_ functions, with a valid pProgram:

Code: OxygenBasic
  1.   sbfun=scriba_LookupFunctionByName(pProgram,"main::callbacka")
  2.   if sbfun=0 then print "Unable to locate callbackA" : goto ending
  3.   string s={"One","Two","Three","Four"}
  4.   args=scriba_NewSbArgs(pProgram,"s s s s",s[0],s[1],s[2],s[3])
  5.   count=4
  6.   iError=scriba_CallArgEx(pProgram, sbfun, @ReturnData, count, args)
  7.   scriba_DestroySbArgs(pProgram,args,4)
  8.  

Passing integers {100,200,300,400}, is simpler: Here it is done with an array of longs, instead of using a set of SB data blocks: (2 signifies integer type)


Code: OxygenBasic
  1.   sbfun=scriba_LookupFunctionByName(pProgram,"main::callbacka")
  2.   if sbfun=0 then print "Unable to locate callbackA" : goto ending
  3.   long a={2,4,100,0, 2,4,200,0, 2,4,300,0, 2,4,400,0} : args=@a
  4.   count=4
  5.   iError=scriba_CallArgEx(pProgram, sbfun, @ReturnData, count, args)
  6.  

By the way, I've tested this with your 64bit libScriba.dll (GCC 2013) :)

Quote from: Charles Pegge
Yes, quite easy. You must check the format of the returned data, and use accordingly.

NB! scriba_DestroySbArgs should not be used on the returned data pointer. It will crash.

Code: OxygenBasic
  1.   'READ RETURNED VALUE
  2.  '-------------------
  3.  '
  4.  select ReturnData.typ
  5.     case SBT_UNDEF  : pr+= "Undefined "
  6.     case SBT_DOUBLE : pr+="double "  ReturnData.dbl
  7.     case SBT_LONG   : pr+="long "    ReturnData.lng
  8.     case SBT_STRING : pr+="zstring " ReturnData.str
  9.     case SBT_ZCHAR  : pr+="zchar "   ReturnData.lng
  10.   end select
  11.  

Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #9 on: April 28, 2015, 07:58:45 PM »
To make this simple, what if we predefine the ArgData data array being passed to scriba_CallArgEx to a maximum of 6 script arguments. What I don't know is if SB will complain passing an array larger than it needs based on the cArgs argument. (number of arguments)


Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #10 on: April 28, 2015, 11:19:18 PM »
More clues to the mystery from past requests on O2 forum.

Quote from: John
Charles,

hook_CallScribaFunction is called by the macro besHOOK_CALLSCRIBAFUNCTION. This function exposes pExecuteObject pEo . Is this the same as the pProgram pointer I'm passing?

Quote from: Charles Pegge
scriba_callArgEx:

not the same, since it passes sbData structures, rather than individual values.

typedef struct _SbData {
  unsigned char type;
  unsigned long size;
  union {
    double d;
    long   l;
    unsigned char *s;
    } v;
  } SbData, *pSbData;


int (*scriba_CallArgEx)(pSbProgram pProgram, unsigned long lEntryNode, pSbData ReturnValue, unsigned long cArgs, pSbData Args);

     #define besScribaCallArgEx(F0,F1,F2,F3,F4) (pSt->scriba_CallArgEx((F0),(F1),(F2),(F3),(F4)))


besHOOK_CALLSCRIBAFUNCTION params: :o

typedef unsigned char BYTE, *PBYTE;

typedef struct _FixSizeMemoryObject {

  union _fsmoval{
    PBYTE  pValue;
    long   lValue;
    double dValue;
    struct _FixSizeMemoryObject **aValue;
    } Value;
  unsigned long Size;
  BYTE sType;
             
  BYTE vType;
  BYTE State;
 
 
 
 
  struct _FixSizeMemoryObject *next;     
  union {
    struct _FixSizeMemoryObject *prev;   
    struct _FixSizeMemoryObject **rprev;
    }link;
  long ArrayLowLimit, ArrayHighLimit;   
                                         
  } FixSizeMemoryObject, *pFixSizeMemoryObject,
    *MortalList, **pMortalList;


from your iCall:

besHOOK_CALLSCRIBAFUNCTION

  besHOOK_CALLSCRIBAFUNCTION(ulEntryPoint,
                             pArgument->Value.aValue,
                             besARGNR-1,
                             &FunctionResult);

It uses the pEo to access the pHookers function table

(pSt->pEo->pHookers->HOOK_CallScribaFunction(pSt->pEo,(ulEntryPoint),(pArgument->Value.aValue),((pParameters ? pParameters->ArrayHighLimit : 0)-1),(&FunctionResult)))


Unfortunately, I appear to have an incomplete pEo / ExecuteObject with many nulls, including the pHookers pointer.
« Last Edit: April 29, 2015, 10:21:58 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #11 on: April 29, 2015, 12:17:01 PM »
Here is my latest brute force attempt.

Code: C
  1. besFUNCTION(SB_CallSubArgs)
  2.   DIM AS VARIABLE Argument;
  3.   DIM AS pSbData ArgData, FunctionResult;
  4.   DIM AS unsigned long sbobj;
  5.   DIM AS int i, slen, fnsn;
  6.  
  7.   Argument = besARGUMENT(1);
  8.   besDEREFERENCE(Argument);
  9.   sbobj = LONGVALUE(Argument);
  10.  
  11.   Argument = besARGUMENT(2);
  12.   besDEREFERENCE(Argument);
  13.   fnsn = LONGVALUE(Argument);
  14.  
  15.   ArgData = besNEWARRAY(0,besARGNR-3);
  16.   for( i=3 ; i <= (unsigned)besARGNR ; i++ ){
  17.     Argument = besARGUMENT(i);
  18.     besDEREFERENCE(Argument);
  19.     switch( slen=TYPE(Argument) ){
  20.       case VTYPE_LONG:
  21.         ArgData[i-3].type = SBT_LONG;
  22.         ArgData[i-3].size = 0;
  23.         ArgData[i-3].v.l = LONGVALUE(Argument);
  24.         break;
  25.       case VTYPE_DOUBLE:
  26.         ArgData[i-3].type = SBT_DOUBLE;
  27.         ArgData[i-3].size = 0;
  28.         ArgData[i-3].v.d = DOUBLEVALUE(Argument);
  29.         break;
  30.       case VTYPE_STRING:
  31.         ArgData[i-3].type = SBT_STRING;
  32.         ArgData[i-3].size = strlen(STRINGVALUE(Argument));
  33.         ArgData[i-3].v.s = STRINGVALUE(Argument);
  34.         break;
  35.     }
  36.   }
  37.   scriba_CallArgEx(sbobj, fnsn, &FunctionResult, besARGNR-2, ArgData);
  38. /*
  39.   for( i=3 ; i <= (unsigned)besARGNR ; i++ ){
  40.      ArgData[i-3] = NULL;
  41.      }
  42. */
  43.   besRELEASE(ArgData);
  44.   besRELEASE(FunctionResult);
  45. besEND
  46.  

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_Run ALIAS "SB_Run" LIB "sbt"
  5. DECLARE SUB SB_NoRun ALIAS "SB_NoRun" LIB "sbt"
  6. DECLARE SUB SB_GetInt ALIAS "SB_GetInt" LIB "sbt"
  7. DECLARE SUB SB_GetDbl ALIAS "SB_GetDbl" LIB "sbt"
  8. DECLARE SUB SB_GetStr ALIAS "SB_GetStr" LIB "sbt"
  9. DECLARE SUB SB_SetInt ALIAS "SB_SetInt" LIB "sbt"
  10. DECLARE SUB SB_SetDbl ALIAS "SB_SetDbl" LIB "sbt"
  11. DECLARE SUB SB_SetStr ALIAS "SB_SetStr" LIB "sbt"
  12. DECLARE SUB SB_Address ALIAS "SB_Address" LIB "sbt"
  13. DECLARE SUB SB_CallSub ALIAS "SB_CallSub" LIB "sbt"
  14. DECLARE SUB SB_CallSubArgs ALIAS "SB_CallSubArgs" LIB "sbt"
  15. DECLARE SUB SB_Destroy ALIAS "SB_Destroy" LIB "sbt"
  16.  
  17. sb = SB_New()
  18. SB_Configure sb, "/etc/scriba/basic.conf"
  19. SB_Load sb, "prtvars.sb"
  20. SB_NoRun sb
  21. fnsn = SB_Address(sb, "main::prtvars")
  22. PRINT fnsn,"\n"
  23. SB_CallSubArgs(sb, fnsn, 123, 1.23, "One,Two,Three")
  24. SB_Destroy sb
  25.  

Code: Script BASIC
  1. SUB prtvars(a, b, c)
  2.   PRINT a,"\n"
  3.   PRINT FORMAT("%g\n", b)
  4.   PRINT c,"\n"
  5. END SUB
  6.  


jrs@laptop:~/sb/sb22/sbt$ scriba testicall.sb
4
*** Error in `scriba': malloc(): memory corruption: 0x0000000000f2fa80 ***
Aborted (core dumped)
jrs@laptop:~/sb/sb22/sbt$


 :-\

Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #12 on: April 29, 2015, 01:28:45 PM »
I had a hunch that maybe it was my string passing causing the core dump. It seems this method is partially working as I can call a SUB in ICALL fashion with LONG and DOUBLE argument value types.


jrs@laptop:~/sb/sb22/sbt$ scriba testicall.sb
4
123
1.23
Segmentation fault (core dumped)
jrs@laptop:~/sb/sb22/sbt$


@Charles - I REM'ed the besRELEASE(FunctionResult); call but that didn't prevent the core dump. It seems to be happening on the return to the hosting SB. :-[

Update: It seems no matter what I try, I can't get by the core dump.

I added besRETURNVALUE = NULL; thinking it was needed.
« Last Edit: April 29, 2015, 05:39:00 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #13 on: April 29, 2015, 06:47:12 PM »
Even though the scriba_CallArgEx function call seems to be working, (returning a long & double) it also seems to be the culprit with causing the segmentation fault (core dump) on the return back to the host SB. If I don't make the scriba_CallArgEx call, the function returns without error. I'm at a loss as to what I might be doing wrong.

Current version of the SB_CallSubArgs ext. module function.

Code: C
  1. besFUNCTION(SB_CallSubArgs)
  2.   DIM AS VARIABLE Argument;
  3.   DIM AS pSbData ArgData, FunctionResult;
  4.   DIM AS unsigned long sbobj;
  5.   DIM AS int i, slen, fnsn;
  6.   DIM AS char *strval;
  7.  
  8.   Argument = besARGUMENT(1);
  9.   besDEREFERENCE(Argument);
  10.   sbobj = LONGVALUE(Argument);
  11.  
  12.   Argument = besARGUMENT(2);
  13.   besDEREFERENCE(Argument);
  14.   fnsn = LONGVALUE(Argument);
  15.  
  16.   ArgData = besNEWARRAY(0,besARGNR-3);
  17.   for( i=3 ; i <= (unsigned)besARGNR ; i++ ){
  18.     Argument = besARGUMENT(i);
  19.     besDEREFERENCE(Argument);
  20.     switch( slen=TYPE(Argument) ){
  21.       case VTYPE_LONG:
  22.         ArgData[i-3].type = SBT_LONG;
  23.         ArgData[i-3].size = 0;
  24.         ArgData[i-3].v.l = LONGVALUE(Argument);
  25.         break;
  26.       case VTYPE_DOUBLE:
  27.         ArgData[i-3].type = SBT_DOUBLE;
  28.         ArgData[i-3].size = 0;
  29.         ArgData[i-3].v.d = DOUBLEVALUE(Argument);
  30.         break;
  31.       case VTYPE_STRING:
  32.         memcpy(&strval,STRINGVALUE(Argument),sizeof(strval));
  33.         ArgData[i-3].type = SBT_STRING;
  34.         ArgData[i-3].size = strlen(strval);
  35.         ArgData[i-3].v.s = strval;
  36.         break;
  37.  
  38.     }
  39.   }
  40.   scriba_CallArgEx(sbobj, fnsn, &FunctionResult, besARGNR-2, ArgData);
  41. /*
  42.   for( i=3 ; i <= (unsigned)besARGNR ; i++ ){
  43.      ArgData[i-3] = NULL;
  44.      }
  45. */
  46.   besRELEASE(ArgData);
  47.   besRELEASE(FunctionResult);
  48.   besRETURNVALUE = NULL;
  49. besEND
  50.  
« Last Edit: April 29, 2015, 06:52:10 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2791
    • ScriptBasic Open Source Project
Re: Script BASIC Extension Module
« Reply #14 on: April 29, 2015, 08:01:56 PM »
Here is a working version of what I'm trying to do in AIR's MBC BASIC to C translator.

I'm using besNEWARRAY to create ArgData where AIR is defining its size at the start.

air_embed.bas
Code: Text
  1. $execon "-I/home/jrs/sb/source /home/jrs/sb/source/lmt_none.c -lscriba"
  2.  
  3. #include <scriba.h>
  4. #include <getopt.h>
  5.  
  6. Dim pProgram As pSbProgram
  7. ' Dim qdat As pSbData
  8. Dim v As long
  9. ' Dim m As long
  10. Dim f1 As long
  11. Dim f2 As long
  12. Dim dVal=11 as long
  13. Dim cArgs As long
  14. Dim ReturnData As SbData
  15. dim ArgData[4] As SbData
  16.  
  17. ' LOADING AND RUNNING THE PROGRAM
  18. pProgram = scriba_new(malloc, free)
  19. scriba_SetFileName(pProgram, "E03.bas")
  20. scriba_LoadSourceProgram(pProgram)
  21. scriba_NoRun(pProgram)
  22.  
  23. '' ACCESSING GLOBAL DATA
  24. v = scriba_LookupVariableByName(pProgram, "main::a")
  25.  
  26. '** NO NEED TO PASS THE VARIABLE TO THE qdat STRUCT
  27. 'scriba_GetVariable(pProgram, v, &qdat)
  28.  
  29. '** NO NEED FOR EXTRA VARIABLE BELOW (OR A SBDATA POINTER TO qdat)
  30. '~ m = qdat->v.l + 100
  31.  
  32. '** BECAUSE WE CAN SET THE VALUE IN THE STRUCT DIRECTLY, IF WE WANT
  33. 'qdat->v.l=100
  34.  
  35. '** THEN PASS IT TO THE FUNCTION
  36. 'scriba_SetVariable(pProgram, v, 2, qdat->v.l, 0, "", 0)
  37.  
  38. '** OR WE CAN PASS THE VALUE WE WANT TO SET TO THE RETRIEVED
  39. '** SCRIPTBASIC "a" VARIABLE >>DIRECTLY<< IN THE FOURTH PARAMETER
  40. scriba_SetVariable(pProgram, v, 2, 500, 0, "", 0)
  41.  
  42. '' CALLING SIMPLE SUBROUTINE
  43. f1 = scriba_LookupFunctionByName(pProgram, "main::dprint")
  44. scriba_Call(pProgram, f1)
  45.  
  46. ' CALLING FUNCTION, RETURNING DATA AND GETTING ALTERED PARAMS
  47. f2 = scriba_LookupFunctionByName(pProgram, "main::eprint")
  48.  
  49. ' SETUP ARGUMENTS - these can be used for both input and output
  50.  
  51. ' ArgData[0].type = SBT_DOUBLE
  52. ' ArgData[1].type = SBT_DOUBLE
  53. ' ArgData[2].type = SBT_DOUBLE
  54. ' ArgData[3].type = SBT_DOUBLE
  55. '
  56. ' ArgData[0].size = 0
  57. ' ArgData[1].size = 0
  58. ' ArgData[2].size = 0
  59. ' ArgData[3].size = 0
  60. '
  61. ' ArgData[0].v.d = 11
  62. ' ArgData[1].v.d = 12
  63. ' ArgData[2].v.d = 13
  64. ' ArgData[3].v.d = 14
  65. '
  66. ' cArgs = 4
  67.  
  68. ' SETUP ARGUMENTS - shorter code
  69.  
  70. for cArgs=0 to 3
  71.   ArgData[cArgs].type = SBT_DOUBLE
  72.   ArgData[cArgs].size = 0
  73.   ArgData[cArgs].v.d = dVal+cArgs
  74. next
  75.  
  76. scriba_CallArgEx(pProgram, f2, &ReturnData, cArgs, ArgData)
  77. print "Return type:",ReturnData.type
  78. print "Value:";
  79.  
  80. ' READ RETURNED VALUE
  81. select case ReturnData.type
  82.  case SBT_UNDEF  :                print "Undefined"
  83.  case SBT_DOUBLE :                print ReturnData.v.d
  84.  case SBT_LONG   :                print ReturnData.v.l
  85.  case =SBT_STRING or =SBT_ZCHAR : print (CHAR PTR)ReturnData.v.s
  86. end select
  87.  
  88. scriba_destroy(pProgram)
  89.  

E03.bas
Code: Script BASIC
  1. ' ----------------
  2. ' GLOBAL VARIABLES
  3. ' ================
  4. '
  5. a=42
  6. b=sqr(2)
  7. s="hello world!"
  8.  
  9. q=dprint()
  10. q=eprint(1,2,3,4)
  11.  
  12. 'print "Enter: "
  13. 'line input q
  14.  
  15. ' ---------
  16. ' FUNCTIONS
  17. ' =========
  18.  
  19. function dprint
  20.   sp="  "
  21.   cr="\n"
  22.   tab=chr$(9)
  23.   print "Dprint Globals: " & s & sp & a & sp & b & cr
  24.   ' line input q
  25.  dprint=1
  26. end function
  27.  
  28. ' """
  29. function eprint(a,b,c,d)
  30.   sp="  "
  31.   cr="\n"
  32.   tab=chr$(9)
  33.   print "Eprint Args: " & a & sp & b & sp & c & sp & d & cr
  34.   ' line input q
  35.  ans=a+b+c+d
  36.   eprint="Sum = " & ans
  37. end function
  38. """
  39. function eprint(s)
  40. PRINT s,"\n"
  41. eprint = "Worked"
  42. end function
  43.  

Output

jrs@laptop:~/mbc$ ./air_embed
Dprint Globals:   500 
1.100000e+01
Return type: 3
Value:Worked
jrs@laptop:~/mbc$

« Last Edit: April 29, 2015, 08:43:11 PM by John »