AI Zone Admin Forum Add your forum

CX Company, chatbot market leader in the Netherlands since 2005, will speak on the Amsterdam Chatbots Meetup on Wednesday, Oct 18!

json gets deleted when using save and restore variables as locals

Because of the other issue I just posted I stopped using locals and continued using save restore variables again.

I want to report an error and warn other users to which may could run into the same problem.

It seems Chatscript autodeletes jsons when a variable is overwritten even if this variable is about to restore to another value later.

Here is a code sample to reproduce the issue:

outputmacro: ^f1()
$test1 = ^jsoncreate(object)
$test1.value TESTVALUE


outputmacro: ^f2(^test
$test1 $$test2)
$test1 blub
$$test2 = ^test

    test2 is 

  [ # 1 ]

$$test1 = ^jsoncreate(PERMANENT object)

default is transient, not saved


  [ # 2 ]

You can execute the test with my line replaced with your line, the bug is still there


  [ # 3 ]

so, I’m not sure i understand what you mean by a save and restore.  Is this crossing a volley boundary or what.
This complaint is currently intermixed with the non-use of $_ variables so I am not clear what is the example.


  [ # 4 ]

I think Tobias means the use of $$test1 in ^f2().
With it being mentioned in the function header then the ordinarily the value of $$test1 is restored to the previous value at the end of ^f2(), but that is not happening when $$test1 initially contained a JSON reference.


  [ # 5 ]

OK. The engine is working as expected, and what you need to change is your coding style.

Initially, CS supported transient global variables ($$x) and permanent global variables ($x) and call by name ^function(^var1). The nature of call-by-name is that what is passed in is not a value, but the original expression. So if one does ^f2($$test.value) then inside ^f2, whenever the compiler sees ^var1, it replaces that with what was actually passed in. So $$x = ^var1 becomes the same as $$x = $$test.value. This applies to assignment as well. Last year save-restore variables were created (the paren list after the argument list) and CS automatically saves the values of them on entry, sets them to null, and restores them on exit. Then pure local variables ($_x) were created. These are implicitly save-restore variables with the more stringent definition that no one called by ^f2 can see them either. They are completely safe.

UNLESS you need to write back onto an incoming value, you should not use ^var1 notation, you should always use $_var1 in order to keep your code safe from side effects.  What actually happened here was, the ^f1 caller used a global transient variable $$test1. That was passed in by name onto ^var1. Then you did save restore on $$test1 and $$test2, so they became null locally. Then you tried to retrieve the value of ^var1, but that is the same as retrievning $$test1.value and $$test1 is now NULL. Or you could use $_ variables when NOT expecting someone lower to use them.. So your ^f1 caller could have been using $_test1 as a variable, passed THAT to ^f2.  CS would have converted that to a call by value call because you are NEVER allowed to access local variables outside your scope, etc.


  login or register to react