forked from Touffy/JSSCxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCxmlExecute.js
270 lines (248 loc) · 8.18 KB
/
SCxmlExecute.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// just declares datamodel variables as undefined
SCxml.prototype.declare=function(element)
{
var c=element.firstElementChild
if(element.tagName=="data")
{
var id=element.getAttribute("id")
if(id in this.datamodel._jsscxml_predefined_)
console.warn("Variable '"+id+"' is shadowing a predefined"
+ "window variable: please never delete it.\nin", element)
else this.datamodel[id]=undefined
}
else while(c)
{
this.declare(c)
c=c.nextElementSibling
}
}
SCxml.prototype.assign=function(left, right)
{
if(left in this.datamodel._jsscxml_predefined_)
this.datamodel._jsscxml_predefined_[left]=right
else this.datamodel[left]=right
}
SCxml.prototype.assign2=function(left, right, element)
{
this.datamodel._x.__assignRight__=right
this.expr(left+" = _x.__assignRight__", element)
delete this.datamodel._x.__assignRight__
}
SCxml.prototype.readParams=function(element, data, alsoContent)
{
for(var c=element.firstElementChild; c; c=c.nextElementSibling)
{
if(c.tagName=="param"){
var name=c.getAttribute("name")
var value=c.getAttribute("expr") || c.getAttribute("location")
if(data.hasOwnProperty(name))
{
if(data[name] instanceof Array)
data[name].push(this.expr(value, c))
else data[name] = [data[name], this.expr(value, c)]
}
else data[name] = this.expr(value, c)
}
else if(alsoContent && c.tagName=="content")
return data=this.readContent(c)
}
return data
}
SCxml.prototype.readContent=function(c)
{
if(c.hasAttribute("expr"))
return this.expr(c.getAttribute("expr"), c)
var value
if(value=c.firstElementChild){ // XML content
if(value==c.lastElementChild){
var tmp=sc.dom.implementation.createDocument(
value.namespaceURI, value.localName)
for(var c=value.firstChild; c; c=c.nextSibling)
tmp.documentElement.appendChild(tmp.importNode(c, true))
return tmp
}
value=this.dom.createDocumentFragment()
for(var c=element.firstChild; c; c=c.nextSibling)
value.appendChild(c.cloneNode(true))
return value
}
if(value=c.textContent){ // JSON or normalized text content
try{return JSON.parse(value)} // see if it is valid JSON
catch(err){return value.replace(/^\s*|\s*$/g, "").replace(/\s+/g," ")}
}
}
SCxml.parseTime=function (s)
{
s=/^((?:\d*\.)?\d+)(m?s)$/.exec(s)
if(!s) return -1
var t=Number(s[1])
if(s[2]=="s") t*=1000
return t
}
// handles executable content, including custom namespaced (or not)
SCxml.prototype.execute=function(element)
{
if(element.namespaceURI==this.dom.documentElement.namespaceURI){
if(element.localName in SCxml.executableContent)
return SCxml.executableContent[element.localName](this, element)
else if(element.localName in SCxml.executableContentNS.tolerate){
console.warn("executable element <"+element.tagName+"> should not use default namespace")
return SCxml.executableContentNS.tolerate[element.localName](this, element)
}
else{
var c=element.firstElementChild
while(c)
{
this.execute(c)
c=c.nextElementSibling
}
}
}
else{
if(element.namespaceURI in SCxml.executableContentNS){
if(element.localName in SCxml.executableContentNS[element.namespaceURI])
return SCxml.executableContentNS[element.namespaceURI][element.localName](this, element)
else console.warn("executable element <"+element.tagName+"> is not defined in namespace "+element.namespaceURI)
}
else if(element.localName in SCxml.executableContentNS.tolerate){
console.warn("executable element <"+element.tagName+"> should use its own namespace")
return SCxml.executableContentNS.tolerate[element.localName](this, element)
}
else console.warn("missing executable content extension for namespace "+element.namespaceURI+" used with element <"+element.tagName+">")
}
}
// if you want to add a custom executable element, add a property
// to this object named for your namespace URI, and within that,
// a method with the name of your element
SCxml.executableContentNS={tolerate:{}}
SCxml.executableContent={
raise: function(sc, element)
{
var event=element.getAttribute("event")
||sc.expr(element.getAttribute("eventexpr"))
event=new SCxml.InternalEvent(event, element)
sc.html.dispatchEvent(new CustomEvent("queue", {detail:event}))
sc.internalQueue.push(event)
},
send: function(sc, element)
{
if(sc.sendNoMore) return; // prevent <send> from terminated SCs
var target=element.getAttribute("target")
||sc.expr(element.getAttribute("targetexpr"), element)
var event=element.getAttribute("event")
||sc.expr(element.getAttribute("eventexpr"), element)
if(!element.hasAttribute('id'))
element.setAttribute('id', sc.uniqId())
var id=element.getAttribute("id"), loc
if(loc=element.getAttribute("idlocation"))
sc.expr(loc+'="'+id+'"')
var proc=element.getAttribute("type")
||sc.expr(element.getAttribute("typeexpr"), element)
||"SCXML"
var delay=SCxml.parseTime(element.getAttribute("delay")
|| sc.expr(element.getAttribute("delayexpr"), element))
if(target=="#_internal"){
var e=new SCxml.InternalEvent(event, element)
sc.html.dispatchEvent(new CustomEvent("queue", {detail:e}))
sc.internalQueue.push(e)
return
}
if(proc in SCxml.EventProcessors)
proc=SCxml.EventProcessors[proc]
else
for(var st in SCxml.EventProcessors)
if(SCxml.EventProcessors[st].name==proc)
proc=SCxml.EventProcessors[st]
if("object" != typeof proc)
sc.error("execution",element,
new Error('unsupported IO processor "'+proc+'"'))
var namelist=element.getAttribute("namelist")
var data={}
if(namelist)
{
namelist=namelist.split(" ")
for(var i=0, name; name=namelist[i]; i++)
data[name]=sc.expr(name)
}
data=sc.readParams(element, data, true)
var e=proc.createEvent(event, sc, data, element)
if(delay > -1)
(element.sent || (element.sent=[])).push(
new Delay(delay, !sc.paused, sc, proc, e, target, element))
else proc.send(e, target, element, sc)
},
cancel: function(sc, element)
{
var id=element.getAttribute("sendid")
||sc.expr(element.getAttribute("sendidexpr"))
for(var timer, sent=sc.dom.querySelector("send[id="+id+"]").sent;
timer=sent.pop(); timer.cancel());
},
log: function(sc, element)
{
var value=element.getAttribute("expr")
sc.log(element.getAttribute("label")+" = "+sc.expr(value,element))
},
data: function(sc, element)
{
var id=element.getAttribute("id")
// create the variable first, so it's "declared"
// even if the assignment part fails or doesn't occur
if(id in sc.datamodel._jsscxml_predefined_)
console.warn("Variable '"+id+"' is shadowing a predefined"
+ "window variable: please never delete it.\nin", element)
else sc.datamodel[id]=undefined
if(element.hasAttribute("src"))
console.warn("You should use <fetch> instead of <data src>, which may render the interpreter unresponsive.")
else sc.assign(id, sc.readContent(element))
},
assign: function(sc, element)
{
var loc=element.getAttribute("location")
if(!loc) sc.error("syntax",element,new Error("'location' attribute required"))
value=sc.readContent(element)
sc.assign2(loc, value)
if(sc.expr(loc, element) != value)
sc.error("execution",element,new Error("cannot assign to read-only property"))
},
"if": function(sc, element)
{
var cond=sc.expr(element.getAttribute("cond"))
var c=element.firstElementChild
while(!cond && c)
{
if(c.tagName=="else") cond=true
if(c.tagName=="elseif") cond=sc.expr(c.getAttribute("cond"))
c=c.nextElementSibling
}
while(c)
{
if(c.tagName=="else" || c.tagName=="elseif") break
sc.execute(c)
c=c.nextElementSibling
}
},
foreach: function(sc, element)
{
var a=sc.expr(element.getAttribute("array"))
var v=element.getAttribute("item")
var i=element.getAttribute("index")
if(("object"!=typeof a) && ("string"!=typeof a))
sc.error("execution",element,new TypeError("Invalid array"))
if(i && !/^(\$|[^\W\d])[\w$]*$/.test(i))
sc.error("execution",element,new SyntaxError("Invalid index"))
if(v && !/^(\$|[^\W\d])[\w$]*$/.test(v))
sc.error("execution",element,new SyntaxError("Invalid item"))
for(var k in a)
{
if(i) sc.assign(i,k)
if(v) sc.assign(v,a[k])
for(var c=element.firstElementChild; c; c=c.nextElementSibling)
sc.execute(c)
}
},
script: function(sc, element)
{
sc.wrapScript(element.textContent,element)
}
}