'Global' EnableJS

Got an idea for enhancing SpiderBasic? New command(s) you'd like to see?
ljgww
Posts: 26
Joined: Thu Mar 26, 2020 5:47 pm

'Global' EnableJS

Post by ljgww »

I have encountered obscure issue but with interesting use case.

Idea:

Make a JavaScript function on a 'global' level or even a library of Javascript functions that can be used from .sb code.

Contemplate following .sb code:

Code: Select all

EnableJS
function someJSfunction(text)
{
console.log(text);
}
DisableJS

; Function adapter
Procedure myCall(text.s)
  Debug "calling myCall"
  EnableJS
  someJSfunction(v_text);  
  DisableJS
  Debug "finished myCall"
EndProcedure

;Actually make a function call
Debug "start"
myCall("hello world")
Debug "end"
Code compiles OK

When you run the code in debug window you get
start
calling myCall
then execution stops.
It is expected to print "Hello World" in browser console and come to an 'end' in Debug window.

Inspect browser console. You get:
ReferenceError: someJSfunction is not defined
Inspect compiled JavaScript code:

Code: Select all

// fragment of Javascript as compiled

function f_mycall(v_text) {
spider.debug.Print(_S1);
  someJSfunction(v_text);  
return 0;
}
//
SpiderLaunch = function() {
  spider.debug.Init();
 spider_InitFunctions();
function someJSfunction(text)
{
console.log(test);
}
spider.debug.Print(_S2);
f_mycall(_S3);
spider.debug.Print(_S4);

}
You may find that function is actually placed in code. So what is the problem?

Found out that the problem is the 'position' or 'location' of the function (or perhaps we can call it 'closure' where compiled code is placed as it is placed in logical place of 'global' code enclosure )

Hack the javascript compilation to (take function out from the 'global' enclosure:

Code: Select all

// Hacked spiderbasic.js

function someJSfunction(text)
{
console.log(test);
}

function f_mycall(v_text) {
spider.debug.Print(_S1);
  someJSfunction(v_text);  
return 0;
}
//
SpiderLaunch = function() {
  spider.debug.Init();
 spider_InitFunctions();
spider.debug.Print(_S2);
f_mycall(_S3);
spider.debug.Print(_S4);

}
Now it works as desired. :)

Debug window:
start
calling myCall
finished myCall
end
Browser console:

Code: Select all

start debug.js:47:13
calling myCall debug.js:47:13
hello world spiderbasic.js:273:9
finished myCall debug.js:47:13
end
but it is post compile hack that cannot be done from compiler (and from .sb code)

experiments...

Move EnableJS block up and down in the .sb code... it looks like it renders it in the same place always regardless where 'global' code is placed in actual .sb code.

Solution 1: It would be nice to have 'global' JS block before any function compiled OR

Solution 2: better!!, place global JS block in order of appearance in the code - if it is between two procedures, place it there. This way coder can make JS code visible or not according to the placement.

or (perhaps) solution 3: expand EnableJS statement to influence code placement in resulting (compiled) Javascript e.g. one of examples could be to make "EnableJSLibrary" that would render library of JS functions before procedures in .sb code.

Note: It is possible to influence compiling/rendering of JS code by placing EnableJS within Procedure (a technique already used in the sample in 'adapter'), but then it becomes enclosed within Procedure function (JS function is not 'global' in this case and cannot be used)

p.s. EnableExplicit makes no difference (there is no violation of .sb code rules)

perhaps there may be some other way of influencing this block I do not know about.

Cheers!
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: 'Global' EnableJS

Post by Peter »

You can set the function in the window scope. So it can be accessed from everywhere:

Code: Select all

EnableJS
function someJSfunction(text) {
  console.log(text);
}
window.someJSfunction = someJSfunction; // <-- here
DisableJS
ljgww
Posts: 26
Joined: Thu Mar 26, 2020 5:47 pm

Re: 'Global' EnableJS

Post by ljgww »

OK. Got that. Very useful! Shall try it out. And use a technique onwards.

Now.... while toying with pointers of kingwolf71 he asked me - how about a macro instead of a procedure?

Something on the line...

Code: Select all

Macro memtest(dest, n)
  EnableJS
  console.log('hi');
  // add more lines here
  DisableJS  
EndMacro
You better see it yourself (compiled javascript)

Perhaps a new topic?
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: 'Global' EnableJS

Post by Peter »

ljgww wrote:You better see it yourself (compiled javascript)
it looks like SpiderBasic cannot resolve the DisableJS within a Macro. Could be a bug.
ljgww
Posts: 26
Joined: Thu Mar 26, 2020 5:47 pm

Re: 'Global' EnableJS

Post by ljgww »

Peter wrote:it looks like SpiderBasic cannot resolve the DisableJS within a Macro. Could be a bug.
Roger. Over.
Forking the callback with promise :)
Post Reply