-
Notifications
You must be signed in to change notification settings - Fork 1
/
JSProxy.as
182 lines (157 loc) · 6.17 KB
/
JSProxy.as
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.rails2u.bridge {
import flash.utils.Proxy;
import flash.external.ExternalInterface;
import flash.utils.flash_proxy;
public dynamic class JSProxy extends Proxy {
use namespace flash_proxy;
public static var proxyLogger:Function = function(...args):void {};
private var _stack:Array;
private var _nextCallIsstack:Boolean = false;
public var forceAsync:Boolean = false;
public function JSProxy(stack:Array = null, forceAsync:Boolean = false) {
_stack = stack || [];
this.forceAsync = forceAsync;
}
public function get proxy():JSProxy {
return getProperty('proxy');
}
public static function get proxy():JSProxy {
return new JSProxy();
}
flash_proxy override function callProperty(name:*, ...args):* {
var $name:* = $check(name);
if ($name) {
return callJS(newStack($name, args));
} else {
return new JSProxy(newStack(name, args), forceAsync);
}
}
flash_proxy function $check(name:*):* {
if (name.toString().indexOf('$') == 0 && name.toString().length >= 2) {
var r:String = name.toString().replace(/^\$+/, '');
if (name is QName) {
return new QName(name.uri, r);
} else {
return r;
}
} else {
return '';
}
}
flash_proxy override function getProperty(name:*):* {
var $name:* = $check(name);
if ($name) {
return callJS(newStack($name));
} else {
return new JSProxy(newStack(name), forceAsync);
}
}
flash_proxy override function getDescendants(name:*):* {
return callJS(newStack(name));
}
flash_proxy function newStack(name:*, args:Array = null):Array {
var stack:Array = _stack.slice();
if (args != null) {
stack.push([name, args.slice()]);
} else {
stack.push(name);
}
return stack;
}
flash_proxy override function hasProperty(name:*):Boolean {
return callJS(newStack(new QName('', name))) !== undefined;
}
flash_proxy override function setProperty(name:*, value:*):void {
var $name:* = $check(name);
if ($name) {
callJSSetProperty(newStack($name), value);
} else {
callJSSetProperty(newStack(name), value);
}
}
flash_proxy override function deleteProperty(name:*):Boolean {
return false;
}
public function toString():String {
var res:Array = createCommand(_stack.slice());
var cmd:String = res[0];
var args:Array = res[1];
return '[JSProxy] ' + cmd + ' :: ' + args.join(', ');
}
flash_proxy function callJSSetProperty(stack:Array, value:*):void {
if (!ExternalInterface.available)
return;
var res:Array = createCommand(stack);
var cmd:String = res[0];
var args:Array = res[1];
args.push(value);
var argsString:String = createArgsString(args.length);
//cmd = "(function(" + argsString + ") {setTimeout(function(){try{" + cmd + " = _" + (args.length - 1).toString() + "}catch(e){};}, 0);})";
cmd = "(function(" + argsString + ") {" + asyncFunc(cmd + " = _" + (args.length - 1).toString()) + ";})";
proxyLogger(cmd);
ExternalInterface.call.apply(null, [cmd].concat(args));
}
flash_proxy function asyncFunc(cmd:String):String {
return "setTimeout(function(){ try{" + cmd + "} catch(e) {};}, 0)";
}
flash_proxy function callJS(stack:Array):* {
if (!ExternalInterface.available)
return null;
var res:Array = createCommand(stack);
var cmd:String = res[0];
var args:Array = res[1];
var argsString:String = createArgsString(args.length);
var result:*;
if (forceAsync) cmd = asyncFunc(cmd);
if (args.length > 0) {
cmd = "(function(" + argsString + ") {return " + cmd + ";})";
proxyLogger(cmd);
proxyLogger(args);
result = ExternalInterface.call.apply(null, [cmd].concat(args));
} else {
cmd = "(function() {return " + cmd + ";})";
proxyLogger(cmd);
result = ExternalInterface.call(cmd);
}
return forceAsync ? null : result;
}
flash_proxy function createArgsString(len:uint):String {
var res:Array = [];
for (var i:uint = 0; i < len; i++) {
res.push('_' + i);
}
return res.join(', ');
}
flash_proxy function createCommand(stack:Array):Array {
var receivers:Array = [];
var args:Array = [];
var argsStrings:Array = [];
while (stack.length) {
var o:* = stack.shift();
if (o is Array) {
o = o as Array;
var cmd:String = nameToString(o[0]) + '(';
var cmdArgs:Array = [];
var a:Array = o[1].slice();
while (a.length) {
cmdArgs.push('_' + args.length);
args.push(a.shift());
}
cmd += cmdArgs.join(', ');
cmd += ')';
receivers.push(cmd);
} else {
receivers.push(nameToString(o));
}
}
return [receivers.join('.').replace(/\.\[/g, '['), args];
}
flash_proxy function nameToString(name:*):String {
if (name is QName) {
return name.localName.toString();
} else {
return '[' + name.toString() + ']';
}
}
}
}