# Hscript serialization

**URL:** https://community.haxe.org/t/hscript-serialization/1918
**Category:** Haxe
**Tags:** hscript
**Created:** [July 20, 2019, 11:32am UTC](https://community.haxe.org/t/hscript-serialization/1918 "2019-07-20T11:32:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![francescoagati](https://community.haxe.org/user_avatar/community.haxe.org/francescoagati/32/503_2.png) [@francescoagati](https://community.haxe.org/u/francescoagati)
#### Post date: [July 20, 2019, 11:32am UTC](https://community.haxe.org/t/hscript-serialization/1918/1 "2019-07-20T11:32:14Z")

</div>

Hello i Need to expose hscript in an api. I want that an user can run commands hscript from an api with the state of programm hscript serialized between a request and another. What Is the best mode forse serialize the state of a hscript program?

---

<div class="post-metadata">

### Author: ![jcward](https://community.haxe.org/user_avatar/community.haxe.org/jcward/32/115_2.png) [@jcward](https://community.haxe.org/u/jcward)
#### Post date: [July 22, 2019, 10:31pm UTC](https://community.haxe.org/t/hscript-serialization/1918/2 "2019-07-22T22:31:53Z")

</div>

I’m pretty sure Terry Cavanaugh worked on some of these issues wrt hscript (async behavior, etc.) – I thought he had a repo, but I can’t find it just now.

Anyway, one of the difficulties of serializing hscript state is that functions can be part of the state. It wouldn’t be impossible to figure out how to serialize these functions… hscript also has a number of forks – there could be something interesting there: [Forks · HaxeFoundation/hscript · GitHub](https://github.com/HaxeFoundation/hscript/network/members)

Another difficulty is that hscript isn’t really designed for as a REPL (execute one instruction at a time.) But you can make it that way. ([ihx](https://github.com/ianxm/ihx) is a fun little project, ala irb, that is an interactive haxe repl. They chose to re-execute every instruction every time – not great if the instructions have side affects, like appending to a file, or grabbing a timestamp.)

Another way is to use the private `interp.exprReturn` function instead of the `interp.execute` function. Then you could try serializing / deserializing the “state of the interpreter” by storing the “locals” map. (sample below – note that `.locals` and `/exprReturn` both require `@:privateAccess` - aka, subject to breakage.)

This seems to work for simple state / values (as long as there are no functions involved). I’ll have to think a bit on solving for functions – it sounds like an interesting problem. 😃

```haxe
import hscript.*;

class Test {

  public static function main()
  {
    var interp = new hscript.Interp();

    function exec_next_expr(e:String) {
      var parser = new hscript.Parser();
      var ast = parser.parseString(e);

      var result:Dynamic = @:privateAccess interp.exprReturn(ast);

      return result;
    }

    exec_next_expr('var x=4;');
    exec_next_expr('x = 1 + 2*x');
    trace(exec_next_expr('x'));

    // serializing the state, then, is equivalent to "storing the locals"
    var state0:String = @:privateAccess haxe.Serializer.run(interp.locals);

    // Now let's change x for the fun of it
    exec_next_expr('x = 5000;');
    trace(exec_next_expr('x'));

    // Now let's restore the locals
    @:privateAccess interp.locals = haxe.Unserializer.run(state0);
    
    trace('Restored x:');
    trace(exec_next_expr('x'));

    exec_next_expr('var x=4;');
  }
}

```

Run with:

```haxe
> haxe -x Test -lib hscript
Test.hx:20: 9
Test.hx:27: 5000
Test.hx:32: Restored x:
Test.hx:33: 9

```

---

<div class="post-metadata">

### Author: ![francescoagati](https://community.haxe.org/user_avatar/community.haxe.org/francescoagati/32/503_2.png) [@francescoagati](https://community.haxe.org/u/francescoagati)
#### Post date: [July 23, 2019, 9:03am UTC](https://community.haxe.org/t/hscript-serialization/1918/3 "2019-07-23T09:03:33Z")

</div>

thanks,  
but the idea is storing only variables. i will inject the function on startup of the script. i could try also with override of get set and call functions
