AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

JavaScript example blows up
 
 

when I try to run the example code from the JavaScript manual. Chatscript blows up:

Javascript compiling: eval function fib(nif (== 0return 0if (== 1return 1return fib(n-1) + fib(n
-2); function test(ivar res []; for (05i++) { res.push(fib(i)); print(res.join(' ')); }
FATAL 56
uncaught error
PANIC 56
uncaught error (calling abort


I am running the latest version of CS.

Any Ideas?

Stephen G.

 

 
  [ # 1 ]

That may be a good thing about ChatScript.
When JavaScript gets stuck, just blow it up!
( Just joking Stephen. )

 

 
  [ # 2 ]

How is the Fibonacci Sequence considered example code in ChatScript?

 

 
  [ # 3 ]

The code is straight from the javascript.md manual that Bruce wrote and is distributed with ChatScript.

I have other code that is much simpler but exhibits the same behavior.

outputmacro: ^jstestone(^arg1JavaScript permanent call void testPrint int eval

 function 
testPrint(n{
  
print("this blows up");
 
}

u
TEST_TWO (two)
## make call to javascript and output it.
calling with 42 \n ^flushoutput()
^
jstestone(42\n ^flushoutput()
back from calling with 42 
 

 
  [ # 4 ]

fib.js

// fib.js
function fib(n{
    
if (== 0return 0}
    
if (== 1return 1}
    
return fib(n-1) + fib(n-2);
}

function test(i{
     
var res [];
     for (
05i++) {
     res
.push(fib(i));
     
}
 console
.log(res.join(' '));
}

test
(4

console log output:

0 1 1 2 3

This test shows that the JavaScript include file in the CS manual runs (independent of ChatScript).  Hopefully this may help figure out what is going on.  Again, I was just making a joke in my first post, Stephen.

 

 

 
  [ # 5 ]

Hi Bruce

May I debug the JavaScript in your ChatScript manual? 

// fib.js updated
function fib(n{
    
if (== 0return 0}
    
if (== 1return 1}
    
return fib(n-1) + fib(n-2);
}

function test(j{
     
var res [];
     for (
0ji++) {
     res
.push(fib(i));
     
}
 console
.log(res.join(' '));
}

test
(8


“This will cause the JavaScript to be loaded and the JavaScript function test called with an integer value 4. This will, in turn, print out on the console 0 1 1 2 3”

No, actually the 5 in the for loop statement is what prints out: 0 1 1 2 3

Actually:  u: (test) ^testx(4)

would print out:  0 1 1 2

But the test function parameter (i) was not being used, so I changed it to (j) and used it in the for loop.

So for example: test(8)

Prints out: 0 1 1 2 3 5 8 13


Reference:

https://github.com/bwilcox-1234/ChatScript/blob/master/WIKI/ESOTERIC-CHATSCRIPT/ChatScript-Javascript.md

 

 
  [ # 6 ]

The problem isnt with the javascript, it is that the chatscriot engine crashes when the javascript is called.

Maybe it works on lunux but not windows.

 

 
  [ # 7 ]

crash in engine is failure when using eval instead of compile. You can temporarily use compile instead. I will fix the engine and release change in a bit

 

 
  [ # 8 ]

Thanks Bruce!!:-)

 

 
  [ # 9 ]

For those who may come after us.

here is code that works calling javascript.

Please note that this code unnecessarily makes repeated calls to ^jscodeset1
Bruce states that you really should just call this when the bot comes up but extra calls get ignored.

outputmacro: ^readjscodeset1() javascript permanent eval

 function 
testPrint() {
        
return("javascript working now!!")
 
}

outputmacro
: ^jstestprint() javascript permanent call string testPrint

topic
: ~test-javascript KEEP REPEAT (test javascript)

uTEST_ONE (one)
 ^
readjscodeset1()
 ^
jstestprint() 
 

 
  [ # 10 ]

oops,

Please note that this code unnecessarily makes repeated calls to ^jscodeset1

should read

Please note that this code unnecessarily makes repeated calls to ^readjscodeset1

 

 
  [ # 11 ]

>> Stephen Gissendaner: here is code that works calling javascript ...

Yes, it really works. Thanks!

However, I am trying to call JS function with argument{s}. For example, if function testPrint was ...

function testPrint(num) {
return("javascript working now. Argument is: " + num);
}

... how would output macro ^jstestprint look like ?

Supposing an int input-argument, I am trying with:
^jstestprintarg(^param_from_rule) javascript permanent call string testPrintArg(^param_from_rule) int

However, there is no matching!?

Could you please help?

 

 
  [ # 12 ]

I dont understand your code. you declare a javascript function trying to call a cs function?
And you say there is no pattern matching…. which means you have some input to match AND you have some rule that calls your javascript function, all of which are not shown

 

 
  [ # 13 ]

So outside of the JS definitions have the following line:

outputmacro: ^jstestprintarg(^param_from_arg) Javascript permanent call string testPrint int

This CS->JS mapping only needs to specify the name of the JS function, not the entire definition.

 

 
  [ # 14 ]

Unfortunately, it doesn’t work. (Neither Linux nor Windows.)

This is my *.top file:

outputmacro: ^readjscodeset() javascript permanent eval

function 
testPrint(num{
  
return("javascript working - arg is: " num);
}

outputmacro
: ^jstestprintarg(^param_from_rulejavascript permanent call string testPrint int

topic
: ~myjstest KEEP REPEAT []

t
Hi, try the JS test please !

uJSTEST (jstest _*1)
 ^
readjscodeset()
 ^
jstestprintarg(_0

On my side, if the input is jstest 8 the output will be: I don’t know what to say. It’s not (the expected): javascript working - arg is: 8

I still don’t know - how to invoke JS function with arguments?


p.s. Excuse me for wrong code in my previous post. Many attempts + wrong copy/paste = the wrong code. Sorry!

 

 
  [ # 15 ]

Everything looks OK, but I tend to use global variables in my JS function calls, so I would try that.
The interface from CS to JS seems to prefer variables that are passed by reference.

 

 1 2 > 
1 of 2
 
  login or register to react