-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLive.hx
82 lines (69 loc) · 1.59 KB
/
Live.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import hscript.Parser;
import hscript.Interp;
class Live
{
var parser:Parser;
var interp:Interp;
var script:String;
var methods:Dynamic;
public function new()
{
parser = new Parser();
interp = new Interp();
methods = {};
interp.variables.set("trace", function(m){ trace(m); });
interp.variables.set("getProperty", Reflect.getProperty);
interp.variables.set("setProperty", Reflect.setProperty);
interp.variables.set("callMethod", Reflect.callMethod);
load();
#if sys
flash.Lib.current.addEventListener(flash.events.Event.ENTER_FRAME, update);
#end
}
var counter = 0;
function update(_)
{
counter++;
if (counter > 30)
{
counter = 0;
load();
}
}
function load()
{
var url = "../../script.hs";
#if (flash || js)
url += "?r="+Math.round(Math.random()*10000000);
var http = new haxe.Http(url);
http.onData = function(data) {
parse(http.responseData);
haxe.Timer.delay(load, 500);
}
http.onError = function(data) {
parse(http.responseData);
haxe.Timer.delay(load, 500);
}
http.request();
#end
#if sys
url = "../../../../" + url;
var data = sys.io.File.getContent(url);
parse(data);
#end
}
function parse(data:String)
{
if (data == script) return;
script = data;
// trace("parse: " + data);
var program = parser.parseString(script);
methods = interp.execute(program);
}
public function call(instance:Dynamic, method:String, args:Array<Dynamic>)
{
if (Reflect.field(methods, method) == null) return;
interp.variables.set("this", instance);
Reflect.callMethod(instance, Reflect.field(methods, method), args);
}
}