javascript.js 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2020 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. Virtual World Framework Apache 2.0 license (https://github.com/NikolaySuslov/livecodingspace/blob/master/licenses/LICENSE_VWF.md)
  5. */
  6. /// vwf/model/javascript.js is a placeholder for the JavaScript object interface to the
  7. /// simulation.
  8. ///
  9. /// @module vwf/model/javascript
  10. /// @requires vwf/model
  11. /// @requires vwf/kernel/utility
  12. /// @requires vwf/utility
  13. /// @requires vwf/configuration
  14. import { Utility } from "/core/vwf/utility/utility.js";
  15. import { KUtility } from "/core/vwf/utility/kutility.js";
  16. import { Fabric } from "/core/vwf/fabric.js";
  17. class VWFJavaScript extends Fabric {
  18. constructor(module) {
  19. console.log("JavaScript constructor");
  20. super(module, "Model")
  21. }
  22. factory() {
  23. let _self_ = this;
  24. return this.load(
  25. this.module,
  26. {
  27. // This is a placeholder for providing a natural integration between simulation and the
  28. // browser's JavaScript environment.
  29. //
  30. // Within the JavaScript environment, component instances appear as JavaScript objects.
  31. //
  32. // - Properties appear in the "properties" field. Each property contains a getter and
  33. // setter callback to notify the object of property manipulation.
  34. // - Methods appear in "methods".
  35. // - Events appear in "events".
  36. // - "parent" refers to the parent node and "children" is an array of the child nodes.
  37. //
  38. // - Node prototypes use the JavaScript prototype chain.
  39. // - Properties, methods, events, and children may be referenced directly on the node or
  40. // within their respective collections by name when there is no conflict with another
  41. // attribute.
  42. // - Properties support getters and setters that invoke a handler that may influence the
  43. // property access.
  44. // == Module Definition ====================================================================
  45. // -- initialize ---------------------------------------------------------------------------
  46. initialize: function() {
  47. this.nodes = {}; // maps id => new type()
  48. this.protoNode = undefined; // this.nodes[kutility.protoNodeURI] once it exists
  49. this.creatingNode( undefined, 0 ); // global root // TODO: to allow vwf.children( 0 ), vwf.getNode( 0 ); is this the best way, or should the kernel createNode( global-root-id /* 0 */ )?
  50. },
  51. // == Model API ============================================================================
  52. // -- creatingNode -------------------------------------------------------------------------
  53. creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  54. childSource, childType, childIndex, childName, callback /* ( ready ) */ ) {
  55. var self = this;
  56. // Get the prototype node.
  57. var prototype = this.nodes[childExtendsID] || Object.prototype;
  58. // Get the behavior nodes.
  59. var behaviors = ( childImplementsIDs || [] ).map( function( childImplementsID ) {
  60. return self.nodes[childImplementsID];
  61. } );
  62. // For each behavior, create a proxy for this node to the behavior and attach it above
  63. // the prototype, or above the most recently-attached behavior.
  64. behaviors.forEach( function( behavior ) {
  65. prototype = VWFJavaScript.proxiedBehavior.call( self, prototype, behavior );
  66. } );
  67. // Create the node. Its prototype is the most recently-attached behavior, or the
  68. // specific prototype if no behaviors are attached.
  69. var node = this.nodes[childID] = Object.create( prototype );
  70. if ( childID === VWFJavaScript.kutility.protoNodeURI ) {
  71. this.protoNode = node;
  72. }
  73. Object.defineProperty( node, "private", {
  74. value: {} // for bookkeeping, not visible to scripts on the node // TODO: well, ideally not visible; hide this better ("_private", "vwf_private", ?)
  75. } );
  76. Object.defineProperty( node, "id", {
  77. value: childID,
  78. enumerable: true,
  79. } );
  80. Object.defineProperty( node, "uri", { // "this" is node
  81. get: function() {
  82. return self.kernel.uri( this.id );
  83. },
  84. enumerable: true,
  85. } );
  86. node.name = childName;
  87. node.parent = undefined;
  88. Object.defineProperty( node, "parent_", { // "this" is node in get/set
  89. get: function() {
  90. return this.parent;
  91. },
  92. set: function( value ) {
  93. var childIndex;
  94. if ( this.parent ) {
  95. var oldParent = this.parent;
  96. self.kernel.removeChild( this.parent.id, this.id );
  97. childIndex = oldParent.children.indexOf( this );
  98. if ( childIndex != -1 )
  99. oldParent.children.splice( childIndex, 1 );
  100. }
  101. self.kernel.addChild( value.id, this.id, this.name );
  102. this.parent = value;
  103. childIndex = this.parent.children.indexOf( this );
  104. if ( childIndex == -1 )
  105. this.parent.children.push( this );
  106. },
  107. } );
  108. node.source = childSource;
  109. node.type = childType;
  110. Object.defineProperty( node, "logger", {
  111. value: this.logger.for( "#" + ( childName || childIndex || childID ), node ),
  112. enumerable: true,
  113. } );
  114. // Properties.
  115. node.properties = Object.create( prototype.properties || Object.prototype, {
  116. node: { value: node } // for node.properties accessors (non-enumerable) // TODO: hide this better
  117. } );
  118. Object.defineProperty( node.properties, "create", {
  119. value: function( name, value, get, set ) { // "this" is node.properties
  120. return self.kernel.createProperty( this.node.id, name, value, get, set );
  121. }
  122. } );
  123. // Attach the property meta events to `node.properties.{created,initialized,deleted}`.
  124. VWFJavaScript.createEventAccessor.call( this, node.properties, "created", "properties" );
  125. VWFJavaScript.createEventAccessor.call( this, node.properties, "initialized", "properties" );
  126. VWFJavaScript.createEventAccessor.call( this, node.properties, "deleted", "properties" );
  127. node.private.getters = Object.create( prototype.private ?
  128. prototype.private.getters : Object.prototype
  129. );
  130. node.private.setters = Object.create( prototype.private ?
  131. prototype.private.setters : Object.prototype
  132. );
  133. // Methods.
  134. node.methods = Object.create( prototype.methods || Object.prototype, {
  135. node: { value: node } // for node.methods accessors (non-enumerable) // TODO: hide this better
  136. } );
  137. Object.defineProperty( node.methods, "create", {
  138. value: function( name, parameters, body ) { // "this" is node.methods // TODO: also accept create( name, body )
  139. return self.kernel.createMethod( this.node.id, name, parameters, body );
  140. }
  141. } );
  142. // Attach the method meta events to `node.methods.{created,deleted}`.
  143. VWFJavaScript.createEventAccessor.call( this, node.methods, "created", "methods" );
  144. VWFJavaScript.createEventAccessor.call( this, node.methods, "deleted", "methods" );
  145. node.private.bodies = Object.create( prototype.private ?
  146. prototype.private.bodies : Object.prototype
  147. );
  148. // Events.
  149. node.events = Object.create( prototype.events || Object.prototype, {
  150. node: { value: node }, // for node.events accessors (non-enumerable) // TODO: hide this better
  151. } );
  152. // TODO: these only need to be on the base node's events object
  153. Object.defineProperty( node.events, "create", {
  154. value: function( name, parameters ) { // "this" is node.events
  155. return self.kernel.createEvent( this.node.id, name, parameters );
  156. }
  157. } );
  158. // Attach the event meta events to `node.events.{created,deleted}`.
  159. VWFJavaScript.createEventAccessor.call( this, node.events, "created", "events" );
  160. VWFJavaScript.createEventAccessor.call( this, node.events, "deleted", "events" );
  161. // Provide helper functions to create the directives for adding, removing and flushing
  162. // event handlers.
  163. // Add: `node.events.*eventName* =
  164. // node.events.add( handler [, phases ] [, context ] [, callback( listenerID ) ] )`
  165. Object.defineProperty( node.events, "add", {
  166. value: function( handler, phases, context, callback /* listenerID */ ) {
  167. // Interpret `add( handler [, phases|context ], callback )` as
  168. // `add( handler, phases|context|undefined, undefined, callback )`.
  169. if ( typeof context === "function" || context instanceof Function ) {
  170. callback = context;
  171. context = undefined;
  172. } else if ( typeof phases === "function" || phases instanceof Function ) {
  173. callback = phases;
  174. context = phases = undefined;
  175. }
  176. // Interpret `add( handler, context, ... )` as `add( handler, undefined, context, ... )`.
  177. if ( VWFJavaScript.valueIsNode.call( self, phases ) ) {
  178. context = phases;
  179. phases = undefined;
  180. }
  181. return { add: true, handler: handler, phases: phases, context: context, callback: callback };
  182. }
  183. } );
  184. // Remove: `node.events.*eventName* = node.events.remove( listenerID|handler )`
  185. Object.defineProperty( node.events, "remove", {
  186. value: function( listenerID ) {
  187. // For 0.6.23 and earlier, listeners were removed using a direct reference to
  188. // the handler. For 0.6.24 and later a `listenerID` is used. Accept a function
  189. // for compatability with components written for 0.6.23 or earlier. The event
  190. // setter will translate the function to an id.
  191. if ( typeof listenerID === "function" || listenerID instanceof Function ) {
  192. return { remove: true, handler: listenerID };
  193. } else {
  194. return { remove: true, id: listenerID };
  195. }
  196. }
  197. } );
  198. // Flush: `node.events.*eventName* = node.events.flush( context )`
  199. Object.defineProperty( node.events, "flush", {
  200. value: function( context ) {
  201. return { flush: true, context: context };
  202. }
  203. } );
  204. node.private.listeners = {}; // not delegated to the prototype as with getters, setters, and bodies; findListeners() filters recursion
  205. // Children.
  206. node.children = []; // TODO: connect children's prototype like properties, methods and events do? how, since it's an array? drop the ordered list support and just use an object?
  207. Object.defineProperty( node.children, "node", {
  208. value: node // for node.children accessors (non-enumerable) // TODO: hide this better
  209. } );
  210. Object.defineProperty( node.children, "create", {
  211. value: function( name, component, callback /* ( child ) */ ) { // "this" is node.children
  212. // Interpret `node.children.create( name, callback )` as
  213. // `node.children.create( name, undefined, callback )`.
  214. if ( typeof component === "function" || component instanceof Function ) {
  215. callback = component;
  216. component = undefined;
  217. }
  218. // Accept `node.children.create( name )` and treat it as
  219. // `node.children.create( name, {} )`.
  220. component = component || {};
  221. // Make the call. If a callback is provided, wrap it and translate the ID to a
  222. // node reference.
  223. if ( callback ) {
  224. self.kernel.createChild( this.node.id, name, VWFJavaScript.componentKernelFromJS.call( self, component ), undefined, undefined, function( childID ) {
  225. callback.call( node, self.nodes[childID] );
  226. } );
  227. } else {
  228. return self.kernel.createChild( this.node.id, name, VWFJavaScript.componentKernelFromJS.call( self, component ) );
  229. }
  230. }
  231. } );
  232. Object.defineProperty( node.children, "delete", {
  233. value: function( child ) {
  234. if ( typeof child === "string" ) {
  235. child = this.node.children[ child ];
  236. }
  237. return self.kernel.deleteNode( child.id );
  238. }
  239. } );
  240. // Attach the child meta events to `node.children.{added,removed}`.
  241. VWFJavaScript.createEventAccessor.call( this, node.children, "added", "children" );
  242. VWFJavaScript.createEventAccessor.call( this, node.children, "removed", "children" );
  243. // Define the "random" and "seed" functions.
  244. Object.defineProperty( node, "random", { // "this" is node
  245. value: function() {
  246. return self.kernel.random( this.id );
  247. }
  248. } );
  249. Object.defineProperty( node, "seed", { // "this" is node
  250. value: function( seed ) {
  251. return self.kernel.seed( this.id, seed );
  252. }
  253. } );
  254. // Define the "time", "client", and "moniker" properties.
  255. Object.defineProperty( node, "time", { // TODO: only define on shared "node" prototype?
  256. get: function() {
  257. return self.kernel.time();
  258. },
  259. enumerable: true,
  260. } );
  261. Object.defineProperty( node, "client", { // TODO: only define on shared "node" prototype?
  262. get: function() {
  263. return self.kernel.client();
  264. },
  265. enumerable: true,
  266. } );
  267. Object.defineProperty( node, "moniker", { // TODO: only define on shared "node" prototype?
  268. get: function() {
  269. return self.kernel.moniker();
  270. },
  271. enumerable: true,
  272. } );
  273. Object.defineProperty( node, "find", {
  274. value: function( matchPattern, callback /* ( match ) */ ) { // "this" is node
  275. if ( callback ) {
  276. self.kernel.find( this.id, matchPattern, true, function( matchID ) {
  277. callback.call( node, self.nodes[matchID] );
  278. } );
  279. } else { // TODO: future iterator proxy
  280. var findResults = self.kernel.find( this.id, matchPattern, true );
  281. if ( findResults )
  282. return findResults.map( function( matchID ) {
  283. return self.nodes[matchID];
  284. } );
  285. }
  286. }
  287. } );
  288. Object.defineProperty( node, "test", {
  289. value: function( matchPattern, testNode ) { // "this" is node
  290. return self.kernel.test( this.id, matchPattern, testNode.id, true );
  291. }
  292. } );
  293. // Define a "future" proxy so that for any this.property, this.method, or this.event, we
  294. // can reference this.future( when, callback ).property/method/event and have the
  295. // expression evaluated at the future time.
  296. Object.defineProperty( node, "in", { // TODO: only define on shared "node" prototype?
  297. value: function( when, callback ) { // "this" is node
  298. return VWFJavaScript.refreshedFuture.call( self, this, -when, callback ); // relative time
  299. },
  300. enumerable: true,
  301. } );
  302. Object.defineProperty( node, "at", { // TODO: only define on shared "node" prototype?
  303. value: function( when, callback ) { // "this" is node
  304. return VWFJavaScript.refreshedFuture.call( self, this, when, callback ); // absolute time
  305. },
  306. enumerable: true,
  307. } );
  308. Object.defineProperty( node, "future", { // same as "in" // TODO: only define on shared "node" prototype?
  309. get: function() {
  310. return this.in;
  311. },
  312. enumerable: true,
  313. } );
  314. node.private.future = Object.create( prototype.private ?
  315. prototype.private.future : Object.prototype
  316. );
  317. Object.defineProperty( node.private.future, "private", {
  318. value: {
  319. when: 0,
  320. callback: undefined,
  321. change: 0,
  322. }
  323. } );
  324. node.private.change = 1; // incremented whenever "future"-related changes occur
  325. },
  326. // -- initializingNode ---------------------------------------------------------------------
  327. // Invoke an initialize() function if one exists.
  328. initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  329. childSource, childType, childIndex, childName ) {
  330. var node = this.nodes[nodeID];
  331. var child = this.nodes[childID];
  332. var scriptText =
  333. "this.hasOwnProperty( 'initialize' ) && " +
  334. "( typeof this.initialize === 'function' || this.initialize instanceof Function ) && " +
  335. "this.initialize()";
  336. // Call the child's initializer.
  337. try {
  338. ( function( scriptText ) { return eval( scriptText ) } ).call( child, scriptText );
  339. } catch ( e ) {
  340. this.logger.warnx( "initializingNode", childID,
  341. "exception in initialize:", VWFJavaScript.utility.exceptionMessage( e ) );
  342. }
  343. // The node is fully initialized at this point
  344. // Link to the parent.
  345. //
  346. // The parent reference is only defined once the node is fully initialized.
  347. // It is not defined earlier since components should be able to stand alone
  348. // without depending on external nodes.
  349. //
  350. // Additionally, since parts of the application may become ready in a different
  351. // order on other clients, referring to properties in other parts of the
  352. // application may lead to consistency errors.
  353. child.parent = node;
  354. if ( node ) {
  355. node.children[childIndex] = child;
  356. if ( parseInt( childName ).toString() !== childName ) {
  357. node.children[childName] = child;
  358. }
  359. node.hasOwnProperty( childName ) || // TODO: recalculate as properties, methods, events and children are created and deleted; properties take precedence over methods over events over children, for example
  360. ( node[childName] = child );
  361. }
  362. return undefined;
  363. },
  364. // -- initializingNodeFromPrototype --------------------------------------------------------
  365. // Invoke an initialize() function from `childInitializingNodeID` on `childID` if one exists.
  366. initializingNodeFromPrototype: function( nodeID, childID, childInitializingNodeID ) {
  367. var child = this.nodes[childID];
  368. var initializer = this.nodes[childInitializingNodeID];
  369. // Call the prototype's initializer on the child.
  370. try {
  371. var prototypeHasInitialize = ( initializer.hasOwnProperty( 'initialize' ) &&
  372. ( typeof initializer.initialize === 'function' ||
  373. initializer.initialize instanceof Function ) );
  374. if ( prototypeHasInitialize ) {
  375. return initializer.initialize.call( child );
  376. }
  377. } catch ( e ) {
  378. this.logger.warnx( "initializingNodeFromPrototype", childID,
  379. "exception in initialize:", VWFJavaScript.utility.exceptionMessage( e ) );
  380. }
  381. return undefined;
  382. },
  383. // -- deletingNode -------------------------------------------------------------------------
  384. deletingNode: function( nodeID ) {
  385. var child = this.nodes[nodeID];
  386. var node = child.parent;
  387. if ( node ) {
  388. var index = node.children.indexOf( child );
  389. if ( index >= 0 ) {
  390. node.children.splice( index, 1 );
  391. }
  392. delete node.children[child.name]; // TODO: conflict if childName is parseable as a number
  393. if ( node[child.name] === child ) {
  394. delete node[child.name]; // TODO: recalculate as properties, methods, events and children are created and deleted; properties take precedence over methods over events over children, for example
  395. }
  396. child.parent = undefined;
  397. }
  398. delete this.nodes[nodeID];
  399. },
  400. // -- addingChild --------------------------------------------------------------------------
  401. addingChild: function( nodeID, childID, childName ) {
  402. },
  403. // -- removingChild ------------------------------------------------------------------------
  404. removingChild: function( nodeID, childID ) {
  405. var node = this.nodes[nodeID];
  406. var child = this.nodes[childID];
  407. child.parent = undefined;
  408. if ( node ) {
  409. node.children.splice( node.children.indexOf( child ), 1 );
  410. delete node.children[child.name]; // TODO: recalculate as properties, methods, events and children are created and deleted; properties take precedence over methods over events over children, for example
  411. delete node[child.name]; // TODO: recalculate as properties, methods, events and children are created and deleted; properties take precedence over methods over events over children, for example
  412. }
  413. },
  414. // -- creatingProperty ---------------------------------------------------------------------
  415. creatingProperty: function( nodeID, propertyName, propertyValue, propertyGet, propertySet ) {
  416. var node = this.nodes[nodeID];
  417. var self = this;
  418. var getter = propertyGet && // TODO: assuming javascript here; how to specify script type?
  419. VWFJavaScript.functionFromHandler( { body: propertyGet, type: VWFJavaScript.scriptMediaType },
  420. logGetterException );
  421. if ( getter ) {
  422. node.private.getters[ propertyName ] = getter;
  423. } else {
  424. node.private.getters[ propertyName ] = true; // set a guard value so that we don't call prototype getters on value properties
  425. }
  426. var setter = propertySet && // TODO: assuming javascript here; how to specify script type?
  427. VWFJavaScript.functionFromHandler( { parameters: [ "value" ], body: propertySet, type: VWFJavaScript.scriptMediaType },
  428. logSetterException );
  429. if ( setter ) {
  430. node.private.setters[ propertyName ] = setter;
  431. } else {
  432. node.private.setters[ propertyName ] = true; // set a guard value so that we don't call prototype setters on value properties
  433. }
  434. function logGetterException( exception ) {
  435. self.logger.warnx( "creatingProperty", nodeID, propertyName, propertyValue,
  436. "exception evaluating getter:", VWFJavaScript.utility.exceptionMessage( exception ) );
  437. }
  438. function logSetterException( exception ) {
  439. self.logger.warnx( "creatingProperty", nodeID, propertyName, propertyValue,
  440. "exception evaluating setter:", VWFJavaScript.utility.exceptionMessage( exception ) );
  441. }
  442. return this.initializingProperty( nodeID, propertyName, propertyValue );
  443. },
  444. // -- initializingProperty -----------------------------------------------------------------
  445. initializingProperty: function( nodeID, propertyName, propertyValue ) {
  446. var node = this.nodes[nodeID];
  447. VWFJavaScript.createPropertyAccessor.call( this, node.properties, propertyName );
  448. node.hasOwnProperty( propertyName ) || // TODO: recalculate as properties, methods, events and children are created and deleted; properties take precedence over methods over events over children, for example
  449. VWFJavaScript.createPropertyAccessor.call( this, node, propertyName );
  450. // Invalidate the "future" cache.
  451. node.private.change++;
  452. return propertyValue !== undefined ?
  453. this.settingProperty( nodeID, propertyName, propertyValue ) : undefined;
  454. },
  455. // TODO: deletingProperty
  456. // -- settingProperty ----------------------------------------------------------------------
  457. settingProperty: function( nodeID, propertyName, propertyValue ) {
  458. var node = this.nodes[nodeID];
  459. if ( ! node ) return; // TODO: patch until full-graph sync is working; drivers should be able to assume that nodeIDs refer to valid objects
  460. var setter = node.private.setters && node.private.setters[propertyName];
  461. if ( setter && setter !== true ) { // is there is a setter (and not just a guard value)
  462. try {
  463. var valueJS = VWFJavaScript.valueJSFromKernel.call( this, propertyValue );
  464. var resultJS = setter.call( node, valueJS );
  465. return VWFJavaScript.valueKernelFromJS.call( this, resultJS );
  466. } catch ( e ) {
  467. this.logger.warnx( "settingProperty", nodeID, propertyName, propertyValue,
  468. "exception in setter:", VWFJavaScript.utility.exceptionMessage( e ) );
  469. }
  470. }
  471. return undefined;
  472. },
  473. // -- gettingProperty ----------------------------------------------------------------------
  474. gettingProperty: function( nodeID, propertyName, propertyValue ) {
  475. var node = this.nodes[nodeID];
  476. //if ( ! node ) return;
  477. var getter = node.private.getters && node.private.getters[propertyName];
  478. if ( getter && getter !== true ) { // is there is a getter (and not just a guard value)
  479. try {
  480. var resultJS = getter.call( node );
  481. return VWFJavaScript.valueKernelFromJS.call( this, resultJS );
  482. } catch ( e ) {
  483. this.logger.warnx( "gettingProperty", nodeID, propertyName, propertyValue,
  484. "exception in getter:", VWFJavaScript.utility.exceptionMessage( e ) );
  485. }
  486. }
  487. return undefined;
  488. },
  489. // -- creatingMethod -----------------------------------------------------------------------
  490. creatingMethod: function( nodeID, methodName, methodParameters, methodBody ) {
  491. var node = this.nodes[nodeID];
  492. VWFJavaScript.createMethodAccessor.call( this, node.methods, methodName );
  493. node.hasOwnProperty( methodName ) || // TODO: recalculate as properties, methods, events and children are created and deleted; properties take precedence over methods over events over children, for example
  494. VWFJavaScript.createMethodAccessor.call( this, node, methodName );
  495. // Invalidate the "future" cache.
  496. node.private.change++;
  497. // Delegate to `settingMethod`.
  498. return this.settingMethod( nodeID, methodName, {
  499. parameters: methodParameters,
  500. body: methodBody,
  501. type: typeof methodBody === "string" || methodBody instanceof String ?
  502. VWFJavaScript.scriptMediaType : undefined, // TODO: heuristic duplicated in vwf.js `normalizedHandler`.
  503. } );
  504. },
  505. // TODO: deletingMethod
  506. // -- settingMethod ------------------------------------------------------------------------
  507. settingMethod: function( nodeID, methodName, methodHandler ) {
  508. var node = this.nodes[nodeID];
  509. var self = this;
  510. var body = VWFJavaScript.functionFromHandler( methodHandler, logException,
  511. vwf.configuration[ "preserve-script-closures" ] );
  512. if ( body ) {
  513. node.private.bodies[ methodName ] = body;
  514. return VWFJavaScript.handlerFromFunction( body ); // TODO: shortcut to avoid retrieving this?
  515. } else {
  516. delete node.private.bodies[ methodName ];
  517. }
  518. function logException( exception ) {
  519. self.logger.warnx( "settingMethod", nodeID, methodName, methodHandler.parameters,
  520. "exception evaluating body:", VWFJavaScript.utility.exceptionMessage( exception ) );
  521. }
  522. return undefined;
  523. },
  524. // -- gettingMethod ------------------------------------------------------------------------
  525. gettingMethod: function( nodeID, methodName ) {
  526. var node = this.nodes[nodeID];
  527. var body = node.private.bodies && node.private.bodies[methodName];
  528. if ( body ) {
  529. return VWFJavaScript.handlerFromFunction( body );
  530. }
  531. return undefined;
  532. },
  533. // -- callingMethod ------------------------------------------------------------------------
  534. callingMethod: function( nodeID, methodName, methodParameters ) {
  535. var node = this.nodes[nodeID];
  536. var body = node.private.bodies && node.private.bodies[methodName];
  537. if ( body ) {
  538. try {
  539. var parametersJS = VWFJavaScript.parametersJSFromKernel.call( this, methodParameters );
  540. var resultJS = body.apply( node, parametersJS );
  541. return VWFJavaScript.valueKernelFromJS.call( this, resultJS );
  542. } catch ( e ) {
  543. this.logger.warnx( "callingMethod", nodeID, methodName, methodParameters, // TODO: limit methodParameters for log
  544. "exception:", VWFJavaScript.utility.exceptionMessage( e ) );
  545. }
  546. }
  547. return undefined;
  548. },
  549. // -- creatingEvent ------------------------------------------------------------------------
  550. creatingEvent: function( nodeID, eventName, eventParameters ) {
  551. var node = this.nodes[nodeID];
  552. VWFJavaScript.createEventAccessor.call( this, node.events, eventName );
  553. node.hasOwnProperty( eventName ) || // TODO: recalculate as properties, methods, events and children are created and deleted; properties take precedence over methods over events over children, for example
  554. VWFJavaScript.createEventAccessor.call( this, node, eventName );
  555. // Invalidate the "future" cache.
  556. node.private.change++;
  557. },
  558. // -- addingEventListener ------------------------------------------------------------------
  559. addingEventListener: function( nodeID, eventName, eventListenerID, eventHandler, eventContextID, eventPhases ) {
  560. var node = this.nodes[nodeID];
  561. // Create the listeners collection if this is the first listener added for an event on a
  562. // prototype.
  563. if ( ! node.private.listeners[eventName] ) {
  564. node.private.listeners[eventName] = [];
  565. }
  566. // Build a `Listener` from the `Handler` and the context and phases.
  567. var eventListener = VWFJavaScript.utility.merge( eventHandler, {
  568. context: eventContextID,
  569. phases: eventPhases,
  570. } );
  571. // Delegate to `settingEventListener`.
  572. return this.settingEventListener( nodeID, eventName, eventListenerID, eventListener ) ?
  573. true : undefined;
  574. },
  575. // -- removingEventListener ----------------------------------------------------------------
  576. removingEventListener: function( nodeID, eventName, eventListenerID ) {
  577. var node = this.nodes[nodeID];
  578. var listeners = node.private.listeners[eventName];
  579. if ( listeners && listeners[ eventListenerID ] ) {
  580. delete listeners[ eventListenerID ];
  581. return true;
  582. }
  583. return undefined;
  584. },
  585. // -- settingEventListener -----------------------------------------------------------------
  586. settingEventListener: function( nodeID, eventName, eventListenerID, eventListener ) {
  587. var node = this.nodes[nodeID];
  588. var self = this;
  589. var listeners = node.private.listeners[eventName];
  590. var handler = VWFJavaScript.functionFromHandler( eventListener, logException,
  591. vwf.configuration[ "preserve-script-closures" ] );
  592. if ( handler ) {
  593. listeners[ eventListenerID ] = {
  594. handler: handler,
  595. context: eventListener.context,
  596. phases: eventListener.phases,
  597. };
  598. // Kernel actions that set a value allow the driver to modify the value assigned.
  599. // The result of the action is the actually-assigned value reported by the driver.
  600. // Here, we should return a `Listener` derived from the function we just rendered so
  601. // that `kernel.setEvent` will return the same result that a following
  602. // `kernel.getEvent` would. However, since `Function.toString` is relatively heavy,
  603. // we'll just return the incoming value until there is a demonstrated need for the
  604. // precise result.
  605. // return utility.merge( handlerFromFunction( listener.handler ), {
  606. // context: listener.context,
  607. // phases: listener.phases,
  608. // } );
  609. return eventListener;
  610. } else {
  611. delete listeners[ eventListenerID ];
  612. }
  613. function logException( exception ) {
  614. self.logger.warnx( "settingEventListener", nodeID, eventName, eventListenerID,
  615. "exception evaluating listener:", VWFJavaScript.utility.exceptionMessage( exception ) );
  616. }
  617. return undefined;
  618. },
  619. // -- gettingEventListener -----------------------------------------------------------------
  620. gettingEventListener: function( nodeID, eventName, eventListenerID ) {
  621. var node = this.nodes[nodeID];
  622. var listeners = node.private.listeners[eventName];
  623. if ( listeners ) {
  624. var listener = listeners[ eventListenerID ];
  625. return VWFJavaScript.utility.merge( VWFJavaScript.handlerFromFunction( listener.handler ), {
  626. context: listener.context,
  627. phases: listener.phases,
  628. } );
  629. }
  630. return undefined;
  631. },
  632. // -- flushingEventListeners ---------------------------------------------------------------
  633. flushingEventListeners: function( nodeID, eventName, eventContextID ) {
  634. // Prepare the return value
  635. var removedListenerIDs = [];
  636. // Extract the listeners of the specified node
  637. var node = this.nodes[ nodeID ];
  638. var listeners = node.private.listeners[ eventName ];
  639. // If listeners exist for the given eventName, loop through them, removing any for which
  640. // the context is the node specified by the parameter eventContextID
  641. if ( listeners ) {
  642. listeners.forEach( function( listener, listenerID ) {
  643. if ( listener.context === eventContextID ) {
  644. delete listeners[ listenerID ];
  645. removedListenerIDs.push( listenerID );
  646. }
  647. } );
  648. }
  649. return removedListenerIDs;
  650. },
  651. // -- firingEvent --------------------------------------------------------------------------
  652. firingEvent: function( nodeID, eventName, eventParameters ) {
  653. var phase = eventParameters && eventParameters.phase; // the phase is smuggled across on the parameters array // TODO: add "phase" as a fireEvent() parameter? it isn't currently needed in the kernel public API (not queueable, not called by the drivers), so avoid if possible
  654. var node = this.nodes[nodeID];
  655. var listeners = VWFJavaScript.findListeners( node, eventName );
  656. var parametersJS = VWFJavaScript.parametersJSFromKernel.call( this, eventParameters );
  657. var self = this;
  658. // Call the handlers registered for the event, and calculate the logical OR of each
  659. // result. Normally, callers to fireEvent() ignore the handler result, but dispatched
  660. // events use the return value to determine when an event has been handled as it bubbles
  661. // up from its target.
  662. var handled = listeners && listeners.reduce( function( handled, listener ) {
  663. // Call the handler. If a phase is provided, only call handlers tagged for that
  664. // phase.
  665. try {
  666. if ( ! phase || listener.phases && listener.phases.indexOf( phase ) >= 0 ) {
  667. var contextNode = self.nodes[ listener.context ] || self.nodes[ 0 ]; // default context is the global root // TODO: this presumes this.creatingNode( undefined, 0 ) is retained above
  668. var resultJS = listener.handler.apply( contextNode, parametersJS );
  669. var result = VWFJavaScript.valueKernelFromJS.call( self, resultJS );
  670. return handled || result || result === undefined; // interpret no return as "return true"
  671. }
  672. } catch ( e ) {
  673. self.logger.warnx( "firingEvent", nodeID, eventName, eventParameters, // TODO: limit eventParameters for log
  674. "exception:", VWFJavaScript.utility.exceptionMessage( e ) );
  675. }
  676. return handled;
  677. }, false );
  678. return handled;
  679. },
  680. // -- executing ----------------------------------------------------------------------------
  681. executing: function( nodeID, scriptText, scriptType ) {
  682. var node = this.nodes[nodeID];
  683. if ( scriptType == VWFJavaScript.scriptMediaType ) {
  684. try {
  685. var resultJS = ( function( scriptText ) { return eval( scriptText ) } ).call( node, scriptText || "" );
  686. return VWFJavaScript.valueKernelFromJS.call( this, resultJS );
  687. } catch ( e ) {
  688. this.logger.warnx( "executing", nodeID,
  689. ( scriptText || "" ).replace( /\s+/g, " " ).substring( 0, 100 ), scriptType, "exception:", VWFJavaScript.utility.exceptionMessage( e ) );
  690. }
  691. }
  692. return undefined;
  693. },
  694. } );
  695. }
  696. // == Private functions ========================================================================
  697. // -- proxiedBehavior --------------------------------------------------------------------------
  698. static proxiedBehavior( prototype, behavior ) { // invoke with the model as "this" // TODO: this is a lot like createProperty()/createMethod()/createEvent(), and refreshedFuture(). Find a way to merge. // TODO: nodes need to keep a list of proxies on them and callback here to refresh after changes
  699. var self = this;
  700. var proxy = Object.create( prototype );
  701. Object.defineProperty( proxy, "private", {
  702. value: {}
  703. } );
  704. proxy.private.origin = behavior; // the node we're the proxy for
  705. Object.defineProperty( proxy, "id", {
  706. value: behavior.id,
  707. enumerable: true,
  708. } );
  709. proxy.name = behavior.name;
  710. proxy.parent = behavior.parent;
  711. proxy.source = behavior.source;
  712. proxy.type = behavior.type;
  713. proxy.initialize = behavior.initialize;
  714. proxy.properties = Object.create( prototype.properties || Object.prototype, {
  715. node: { value: proxy } // for proxy.properties accessors (non-enumerable) // TODO: hide this better
  716. } );
  717. proxy.private.getters = Object.create( prototype.private ?
  718. prototype.private.getters : Object.prototype
  719. );
  720. proxy.private.setters = Object.create( prototype.private ?
  721. prototype.private.setters : Object.prototype
  722. );
  723. for ( var propertyName in behavior.properties ) {
  724. if ( behavior.properties.hasOwnProperty( propertyName ) ) {
  725. ( function( propertyName ) {
  726. VWFJavaScript.createPropertyAccessor.call( self, proxy.properties, propertyName );
  727. proxy.hasOwnProperty( propertyName ) || // TODO: recalculate as properties, methods, events and children are created and deleted; properties take precedence over methods over events over children, for example
  728. VWFJavaScript.createPropertyAccessor.call( self, proxy, propertyName );
  729. } )( propertyName );
  730. if ( behavior.private.getters.hasOwnProperty( propertyName ) ) {
  731. proxy.private.getters[propertyName] = behavior.private.getters[propertyName];
  732. }
  733. if ( behavior.private.setters.hasOwnProperty( propertyName ) ) {
  734. proxy.private.setters[propertyName] = behavior.private.setters[propertyName];
  735. }
  736. }
  737. }
  738. proxy.methods = Object.create( prototype.methods || Object.prototype, {
  739. node: { value: proxy } // for proxy.methods accessors (non-enumerable) // TODO: hide this better
  740. } );
  741. proxy.private.bodies = Object.create( prototype.private ?
  742. prototype.private.bodies : Object.prototype
  743. );
  744. for ( var methodName in behavior.methods ) {
  745. if ( behavior.methods.hasOwnProperty( methodName ) ) {
  746. ( function( methodName ) {
  747. VWFJavaScript.createMethodAccessor.call( self, proxy.methods, methodName );
  748. proxy.hasOwnProperty( methodName ) || // TODO: recalculate as properties, methods, events and children are created and deleted; properties take precedence over methods over events over children, for example
  749. VWFJavaScript.createMethodAccessor.call( self, proxy, methodName );
  750. } )( methodName );
  751. if ( behavior.private.bodies.hasOwnProperty( methodName ) ) {
  752. proxy.private.bodies[methodName] = behavior.private.bodies[methodName];
  753. }
  754. }
  755. }
  756. proxy.events = Object.create( prototype.events || Object.prototype, {
  757. node: { value: proxy } // for proxy.events accessors (non-enumerable) // TODO: hide this better
  758. } );
  759. proxy.private.listeners = {}; // not delegated to the prototype as with getters, setters, and bodies; findListeners() filters recursion
  760. for ( var eventName in behavior.events ) {
  761. if ( behavior.events.hasOwnProperty( eventName ) ) {
  762. ( function( eventName ) {
  763. VWFJavaScript.createEventAccessor.call( self, proxy.events, eventName );
  764. proxy.hasOwnProperty( eventName ) || // TODO: recalculate as properties, methods, events and children are created and deleted; properties take precedence over methods over events over children, for example
  765. VWFJavaScript.createEventAccessor.call( self, proxy, eventName );
  766. } )( eventName );
  767. }
  768. }
  769. for ( var eventName in behavior.private.listeners ) { // outside of the behavior.events loop as with getters, setters, and bodies; listeners may appear above the event definition
  770. if ( behavior.private.listeners.hasOwnProperty( eventName ) ) {
  771. proxy.private.listeners[eventName] = behavior.private.listeners[eventName];
  772. }
  773. }
  774. proxy.private.future = Object.create( prototype.private ?
  775. prototype.private.future : Object.prototype
  776. );
  777. Object.defineProperty( proxy.private.future, "private", {
  778. value: {
  779. when: 0,
  780. callback: undefined,
  781. change: 0,
  782. }
  783. } );
  784. proxy.private.change = behavior.private.change;
  785. return proxy;
  786. }
  787. // -- refreshedFuture --------------------------------------------------------------------------
  788. static refreshedFuture( node, when, callback ) { // invoke with the model as "this"
  789. var self = this;
  790. if ( Object.getPrototypeOf( node ).private ) {
  791. VWFJavaScript.refreshedFuture.call( this, Object.getPrototypeOf( node ) );
  792. }
  793. var future = node.private.future;
  794. future.private.when = when;
  795. future.private.callback = callback; // TODO: would like to be able to remove this reference after the future call has completed
  796. if ( future.private.change < node.private.change ) { // only if out of date
  797. Object.defineProperty( future, "id", {
  798. value: node.id,
  799. enumerable: true,
  800. } );
  801. future.properties = Object.create( Object.getPrototypeOf( future ).properties || Object.prototype, {
  802. node: { value: future } // for future.properties accessors (non-enumerable) // TODO: hide this better
  803. } );
  804. for ( var propertyName in node.properties ) {
  805. if ( node.properties.hasOwnProperty( propertyName ) ) {
  806. ( function( propertyName ) {
  807. VWFJavaScript.createPropertyAccessor.call( self, future.properties, propertyName );
  808. future.hasOwnProperty( propertyName ) || // TODO: calculate so that properties take precedence over methods over events, for example
  809. VWFJavaScript.createPropertyAccessor.call( self, future, propertyName );
  810. } )( propertyName );
  811. }
  812. }
  813. future.methods = Object.create( Object.getPrototypeOf( future ).methods || Object.prototype, {
  814. node: { value: future } // for future.methods accessors (non-enumerable) // TODO: hide this better
  815. } );
  816. for ( var methodName in node.methods ) {
  817. if ( node.methods.hasOwnProperty( methodName ) ) {
  818. ( function( methodName ) {
  819. VWFJavaScript.createMethodAccessor.call( self, future.methods, methodName, true );
  820. future.hasOwnProperty( methodName ) || // TODO: calculate so that properties take precedence over methods over events, for example
  821. VWFJavaScript.createMethodAccessor.call( self, future, methodName, true );
  822. } )( methodName );
  823. }
  824. }
  825. future.events = Object.create( Object.getPrototypeOf( future ).events || Object.prototype, {
  826. node: { value: future } // for future.events accessors (non-enumerable) // TODO: hide this better
  827. } );
  828. for ( var eventName in node.events ) {
  829. if ( node.events.hasOwnProperty( eventName ) ) {
  830. ( function( eventName ) {
  831. VWFJavaScript.createEventAccessor.call( self, future.events, eventName, undefined, true );
  832. future.hasOwnProperty( eventName ) || // TODO: calculate so that properties take precedence over methods over events, for example
  833. VWFJavaScript.createEventAccessor.call( self, future, eventName, undefined, true );
  834. } )( eventName );
  835. }
  836. }
  837. future.private.change = node.private.change;
  838. }
  839. return future;
  840. }
  841. /// Define a (JavaScript) accessor property on a node or a node's `properties` collection that
  842. /// will manipulate a (VWF) property on the node.
  843. ///
  844. /// Reading `node.properties.name` invokes the `get` accessor, which calls `kernel.getProperty`
  845. /// to return the value for the property on the node. Writing `node.properties.name` invokes
  846. /// the `set` accessor, which calls `kernel.setProperty` to assign a new value to the property
  847. /// on its node.
  848. ///
  849. /// This function must run as a method of the driver. Invoke it as:
  850. /// `createPropertyAccessor.call( driver, container, propertyName )`.
  851. ///
  852. /// @param {Object} container
  853. /// A `node` or `node.properties` object to receive the property.
  854. /// @param {String} propertyName
  855. /// The name of the property to create on `container`.
  856. static createPropertyAccessor( container, propertyName ) {
  857. var self = this;
  858. Object.defineProperty( container, propertyName, {
  859. // On read, call `kernel.getProperty` and return the result.
  860. get: function() { // `this` is the container
  861. var node = this.node || this; // the node via node.properties.node, or just node
  862. var resultKernel = self.kernel.getProperty( node.id, propertyName,
  863. node.private.when, node.private.callback );
  864. return VWFJavaScript.valueJSFromKernel.call( self, resultKernel );
  865. },
  866. // On write, pass the assigned value to `kernel.setProperty`.
  867. set: function( value ) { // `this` is the container
  868. var node = this.node || this; // the node via node.properties.node, or just node
  869. var valueKernel = VWFJavaScript.valueKernelFromJS.call( self, value );
  870. self.kernel.setProperty( node.id, propertyName, valueKernel,
  871. node.private.when, node.private.callback );
  872. },
  873. enumerable: true,
  874. } );
  875. }
  876. /// Define an accessor property on a node or a node's `methods` collection that manipulates a
  877. /// method on the node.
  878. ///
  879. /// Reading `node.methods.name` returns a function that when called calls `kernel.callMethod` to
  880. /// invoke the method on the node. Writing a function object to `node.methods.name` will set the
  881. /// method body to the assigned function.
  882. ///
  883. /// This function must run as a method of the driver. Invoke it as:
  884. /// `createMethodAccessor.call( driver, container, methodName )`.
  885. ///
  886. /// @param {Object} container
  887. /// A `node` or `node.methods` object to receive the property.
  888. /// @param {String} methodName
  889. /// The name of the property to create on `container`.
  890. /// @param {Boolean} [unsettable]
  891. /// When truthy, don't create the `set` accessor. An unsettable method property doesn't allow
  892. /// the method body to be changed.
  893. static createMethodAccessor( container, methodName, unsettable ) {
  894. var self = this;
  895. Object.defineProperty( container, methodName, {
  896. // On read, return a function that calls `kernel.callMethod` when invoked.
  897. get: function() { // `this` is the container
  898. var node = this.node || this; // the node via node.methods.node, or just node
  899. return function( /* parameter1, parameter2, ... */ ) { // `this` is the container
  900. var argumentsKernel = VWFJavaScript.parametersKernelFromJS.call( self, arguments );
  901. var resultKernel = self.kernel.callMethod( node.id, methodName, argumentsKernel,
  902. node.private.when, node.private.callback );
  903. return VWFJavaScript.valueJSFromKernel.call( self, resultKernel );
  904. };
  905. },
  906. // On write, update the method body. `unsettable` methods don't accept writes.
  907. set: unsettable ? undefined : function( value ) { // `this` is the container
  908. var node = this.node || this; // the node via node.methods.node, or just node
  909. self.kernel.setMethod( node.id, methodName,
  910. VWFJavaScript.handlerFromFunction( value, vwf.configuration[ "preserve-script-closures" ] ) );
  911. },
  912. enumerable: true,
  913. } );
  914. }
  915. /// Define an accessor property on a node or a node's `events` collection that manipulates an
  916. /// event on the node. `createEventAccessor` is also used to define accessor properties at other
  917. /// locations on the node to expose the node's meta events.
  918. ///
  919. /// Reading `node.events.name` returns a function that when called calls `kernel.fireEvent` to
  920. /// fire the event from the node. Writing a function object to `node.events.name` will add the
  921. /// assigned function as a new listener using default parameters. To add a listener with
  922. /// specified paramters, call the `node.events.add` helper function and assign the result to
  923. /// `node.events.name`. Remove a listener by calling the `node.events.remove` helper and
  924. /// assigning the result. Flush a set of listeners with the `node.events.flush` helper.
  925. ///
  926. /// This function must run as a method of the driver. Invoke it as:
  927. /// `createEventAccessor.call( driver, container, eventName [, eventNamespace ] [, unsettable ] )`.
  928. ///
  929. /// @param {Object} container
  930. /// A `node` or `node.events` object to receive the property. Meta events will attach to
  931. /// `node.properties`, `node.methods`, `node.events`, and `node.children` as well.
  932. /// @param {String} eventName
  933. /// The name of the property to create on `container`.
  934. /// @param {String} [eventNamespace]
  935. /// For meta events, the namespace associated with the event.
  936. /// @param {Boolean} [unsettable]
  937. /// When truthy, don't create the `set` accessor. An unsettable event property can't add or
  938. /// remove listeners.
  939. static createEventAccessor( container, eventName, eventNamespace, unsettable ) {
  940. var self = this;
  941. Object.defineProperty( container, eventName, {
  942. // On read, return a function that calls `kernel.fireEvent` when invoked. Namespaced
  943. // events (which are meta events and controlled by the kernel) are ungettable and can't
  944. // be fired by the application.
  945. get: eventNamespace ? undefined : function() { // `this` is the container
  946. var node = this.node || this; // the node via node.*collection*.node, or just node
  947. return function( /* parameter1, parameter2, ... */ ) { // `this` is the container
  948. var argumentsKernel = VWFJavaScript.parametersKernelFromJS.call( self, arguments );
  949. var resultKernel = self.kernel.fireEvent( node.id, eventName, argumentsKernel,
  950. node.private.when, node.private.callback );
  951. return VWFJavaScript.valueJSFromKernel.call( self, resultKernel );
  952. };
  953. },
  954. // On write, update the listeners. `unsettable` events don't accept writes.
  955. set: unsettable ? undefined : function( value ) { // `this` is the container
  956. var node = this.node || this; // the node via node.*collection*.node, or just node
  957. var namespacedName = eventNamespace ? [ eventNamespace, eventName ] : eventName;
  958. if ( typeof value === "function" || value instanceof Function ) {
  959. // `container.*eventName* = handler` (context is the target node).
  960. addListener( value, node );
  961. } else if ( value.add ) {
  962. // `container.*eventName* = node.events.add( handler, phases, context, callback /* listenerID */ )`.
  963. if ( ! value.phases || value.phases instanceof Array ) {
  964. addListener( value.handler, value.context, value.phases, value.callback );
  965. } else {
  966. addListener( value.handler, value.context, [ value.phases ], value.callback );
  967. }
  968. } else if ( value.remove ) {
  969. // `container.*eventName* = node.events.remove( listenerID|handler )`.
  970. // For `node.events.remove( listenerID )`, remove using the direct parameter.
  971. // For `node.events.remove( handler )`, use the id that `addListener` attached
  972. // to the handler.
  973. self.kernel.removeEventListener( node.id, namespacedName,
  974. value.handler ? value.handler.listenerID : value.id );
  975. } else if ( value.flush ) {
  976. // `container.*eventName* = node.events.flush( context )`.
  977. self.kernel.flushEventListeners( node.id, namespacedName,
  978. value.context && value.context.id );
  979. }
  980. function addListener( handler, context, phases, callback ) {
  981. var listenerID = self.kernel.addEventListener( node.id, namespacedName,
  982. VWFJavaScript.handlerFromFunction( handler, vwf.configuration[ "preserve-script-closures" ] ),
  983. context && context.id, phases );
  984. // For 0.6.23 and earlier, listeners were removed using a direct reference to
  985. // the handler. For backward compatability, tag the handler with the listener id
  986. // so that we can retrieve the id if the listener is removed by handler.
  987. handler.listenerID = listenerID;
  988. callback && callback.call( node, listenerID );
  989. }
  990. },
  991. // Meta events--including the `properties`, `methods`, and `events` `created` and
  992. // `deleted` events, and the `children` `added` and `removed` events--are not
  993. // enumerable.
  994. enumerable: ! eventNamespace,
  995. } );
  996. }
  997. /// Convert a `Handler` to a JavaScript function.
  998. ///
  999. /// @param {Handler} handler
  1000. /// A `Handler` to convert to a function.
  1001. /// @param {function} [errback]
  1002. /// If `errback` is provided, any exception that occurs during the conversion will be passed
  1003. /// to `errback` as `errback( exception )`.
  1004. /// @param {boolean} [bypass]
  1005. /// Expect that `handler.body` is a function object instead of the string representation of
  1006. /// the function body. Return the function without any conversion. This parameter should only
  1007. /// be used in support of the backwards-compatability `preserve-script-closures` configuration
  1008. /// option.
  1009. ///
  1010. /// @returns {function|undefined}
  1011. /// The function generated from `handler`, or `undefined` if `handler` does not describe a
  1012. /// JavaScript function or if the function could not be converted.
  1013. static functionFromHandler( handler, errback /* exception */, bypass ) {
  1014. if ( bypass && ( typeof handler.body === "function" || handler.body instanceof Function ) ) {
  1015. return handler.body;
  1016. } else if ( handler.type === VWFJavaScript.scriptMediaType ) {
  1017. var name = handler.name, parameters = handler.parameters, body = handler.body;
  1018. var parameterString = parameters && parameters.length ?
  1019. " " + parameters.join( ", " ) + " " :
  1020. "";
  1021. var prefix = "function(" + parameterString + ") {";
  1022. var suffix = "}";
  1023. var functionString, indentedBody;
  1024. if ( body && body.length ) {
  1025. if ( body.charAt( body.length-1 ) === "\n" ) {
  1026. indentedBody = body.match( /^[^\S\n]/ ) ? body : body.replace( /^./gm, " $&" );
  1027. functionString = prefix + "\n" + indentedBody + suffix + "\n";
  1028. } else {
  1029. functionString = prefix + " " + body + " " + suffix;
  1030. }
  1031. } else {
  1032. functionString = prefix + suffix;
  1033. }
  1034. try {
  1035. return eval( "( " + functionString + ")" );
  1036. } catch( exception ) {
  1037. errback && errback( exception );
  1038. }
  1039. }
  1040. return undefined;
  1041. }
  1042. /// Convert a JavaScript `function` to a `Handler`.
  1043. ///
  1044. /// @param {function} funcshun
  1045. /// A function to convert to a `Handler`.
  1046. /// @param {boolean} [bypass]
  1047. /// Create an object having the form of a `Handler`, but with the `body` field set to the
  1048. /// function object instead of the string representation of the function body. The `name`,
  1049. /// `parameters`, and `type` fields will not be set. This parameter should only be used in
  1050. /// support of the backwards-compatability `preserve-script-closures` configuration option.
  1051. ///
  1052. /// @returns {Handler|undefined}
  1053. /// The `Handler` generated from `funcshun`, or `undefined` if the function's `toString` could
  1054. /// not be parsed.
  1055. static handlerFromFunction( funcshun, bypass ) {
  1056. var name, parameters, body, type = VWFJavaScript.scriptMediaType;
  1057. var match, leadingMatch, trailingMatch, indention = "";
  1058. if ( bypass ) {
  1059. return {
  1060. body: funcshun,
  1061. };
  1062. } else if ( match = /* assignment! */ VWFJavaScript.functionRegex.exec( funcshun.toString() ) ) {
  1063. name = match[1];
  1064. // Trim the parameter string. Also remove the `/**/` that Chrome adds to the parameter
  1065. // list for functions created using `Function( parameter, ..., body )`. See
  1066. // `NewFunctionString` in http://code.google.com/p/v8/source/browse/trunk/src/v8natives.js.
  1067. var parameterString = match[2].replace( /\/\*.*\*\//, "" ).trim();
  1068. parameters = parameterString.length ? parameterString.split( "," ).map( function( parameter ) {
  1069. return parameter.trim();
  1070. } ) : undefined;
  1071. // Trim the body string. Recognize block vs. inline formatting where possible and retain
  1072. // the existing spacing.
  1073. body = match[3];
  1074. leadingMatch = // leading spacing, if the leading brace is on its own line
  1075. body.match( /^([^\S\n]*\n)([^\S\n]*)/ );
  1076. trailingMatch = // trailing spacing, if the trailing brace is on own line
  1077. body.match( /\n([^\S\n]*)$/ );
  1078. // Trim the leading spaces. If the leading brace was on its own line, delete the empty
  1079. // first line and take the body indention to be the next line's spacing. Otherwise, just
  1080. // trim the beginning of the body.
  1081. if ( leadingMatch ) {
  1082. body = body.substr( leadingMatch[1].length );
  1083. indention = leadingMatch[2];
  1084. } else {
  1085. body = body.replace( /^\s*/, "" );
  1086. }
  1087. // Trim the trailing spaces. If the trailing brace was on its own line, delete its
  1088. // indention and take that as the body indention. The trailing brace indention takes
  1089. // priority over indention taken from the leading line. If the trailing brace was not on
  1090. // its own line, just trim the end of the body.
  1091. if ( trailingMatch ) {
  1092. body = body.substr( -trailingMatch[1] );
  1093. indention = trailingMatch[1];
  1094. } else {
  1095. body = body.replace( /\s*$/, "" );
  1096. }
  1097. // If we recognized the body as an block (not inline with the braces), unindent it and
  1098. // ensure that the last line ends with a newline.
  1099. if ( leadingMatch || trailingMatch ) {
  1100. body = body.replace( new RegExp( "^" + indention, "gm" ), "" );
  1101. body = body.replace( /\n?$/, "\n" );
  1102. }
  1103. return {
  1104. name: name,
  1105. parameters: parameters,
  1106. body: body,
  1107. type: type,
  1108. };
  1109. }
  1110. return undefined;
  1111. }
  1112. // -- findListeners ----------------------------------------------------------------------------
  1113. // TODO: this walks the full prototype chain and is probably horribly inefficient.
  1114. static findListeners( node, eventName, targetOnly ) {
  1115. var prototypeListeners = Object.getPrototypeOf( node ).private ? // get any self-targeted listeners from the prototypes
  1116. VWFJavaScript.findListeners( Object.getPrototypeOf( node ), eventName, true ) : [];
  1117. var nodeListeners = node.private.listeners && node.private.listeners[eventName] || [];
  1118. if ( targetOnly ) {
  1119. return prototypeListeners.concat( nodeListeners.filter( function( listener ) {
  1120. return listener.context === node.id || // in the prototypes, select self-targeted listeners only
  1121. ( node.private.origin && listener.context === node.private.origin.id );
  1122. } ) );
  1123. } else {
  1124. return prototypeListeners.map( function( listener ) { // remap the prototype listeners to target the node
  1125. return { handler: listener.handler, context: node.id, phases: listener.phases };
  1126. } ).concat( nodeListeners );
  1127. }
  1128. }
  1129. /// Transform node references in a component descriptor into kernel-style node references. The
  1130. /// resulting object will be suitable for passing to `kernel.createNode`.
  1131. ///
  1132. /// This function must run as a method of the driver. Invoke it as:
  1133. /// `componentKernelFromJS.call( driver, component )`.
  1134. ///
  1135. /// @param {String|Object} component
  1136. /// A component URI or descriptor. A URI will pass through unchanged (as will all descriptor
  1137. /// fields that aren't node references.)
  1138. ///
  1139. /// @returns {String|Object}
  1140. /// `component` with node references replaced with kernel-style node references.
  1141. static componentKernelFromJS( component ) {
  1142. return VWFJavaScript.valueKernelFromJS.call( this, component );
  1143. }
  1144. /// Convert a parameter array of values using `valueKernelFromJS`.
  1145. ///
  1146. /// This function must run as a method of the driver. Invoke it as:
  1147. /// `parametersKernelFromJS.call( driver, parameters )`.
  1148. ///
  1149. /// @param {Object[]} parameters
  1150. ///
  1151. /// @returns {Object}
  1152. static parametersKernelFromJS( parameters ) {
  1153. return VWFJavaScript.valueKernelFromJS.call( this, parameters );
  1154. }
  1155. /// Convert a parameter array of values using `valueJSFromKernel`.
  1156. ///
  1157. /// This function must run as a method of the driver. Invoke it as:
  1158. /// `parametersJSFromKernel.call( driver, parameters )`.
  1159. ///
  1160. /// @param {Object[]} parameters
  1161. ///
  1162. /// @returns {Object}
  1163. static parametersJSFromKernel( parameters ) {
  1164. return VWFJavaScript.valueJSFromKernel.call( this, parameters );
  1165. }
  1166. /// Convert node references into special values that can pass through the kernel. These values
  1167. /// are wrapped in such a way that they won't be confused with any other application value, and
  1168. /// they will be replicated correctly by the kernel.
  1169. ///
  1170. /// Other values are returned unchanged. Use `valueJSFromKernel` to retrieve the original.
  1171. ///
  1172. /// This function must run as a method of the driver. Invoke it as:
  1173. /// `valueKernelFromJS.call( driver, value )`.
  1174. ///
  1175. /// @param {Object} value
  1176. ///
  1177. /// @returns {Object}
  1178. static valueKernelFromJS( value ) {
  1179. var self = this;
  1180. return VWFJavaScript.utility.transform( value, function( object, names, depth, finished ) {
  1181. if ( VWFJavaScript.valueIsNode.call( self, object ) ) {
  1182. finished();
  1183. return VWFJavaScript.kutility.nodeReference( object.id );
  1184. } else if ( object && object.buffer && object.buffer.toString() === "[object ArrayBuffer]" ) {
  1185. finished();
  1186. return object;
  1187. } else if ( VWFJavaScript.kutility.valueIsNodeReference( object ) ) {
  1188. finished();
  1189. self.logger.warnx( "valueKernelFromJS", "javascript-format value contains a kernel-format node reference" );
  1190. return object;
  1191. } else {
  1192. return object;
  1193. }
  1194. } );
  1195. }
  1196. /// Convert values wrapped by `valueKernelFromJS` into their original form for use in the
  1197. /// JavaScript driver's execution environment.
  1198. ///
  1199. /// This function must run as a method of the driver. Invoke it as:
  1200. /// `valueJSFromKernel.call( driver, value )`.
  1201. ///
  1202. /// @param {Object} value
  1203. ///
  1204. /// @returns {Object}
  1205. static valueJSFromKernel( value ) {
  1206. var self = this;
  1207. return VWFJavaScript.utility.transform( value, function( object, names, depth, finished ) {
  1208. if ( VWFJavaScript.kutility.valueIsNodeReference( object ) ) {
  1209. finished();
  1210. return self.nodes[ object.id ];
  1211. } else if ( object && object.buffer && object.buffer.toString() === "[object ArrayBuffer]" ) {
  1212. finished();
  1213. return object;
  1214. } else if ( VWFJavaScript.valueIsNode.call( self, object ) ) {
  1215. finished();
  1216. self.logger.warnx( "valueJSFromKernel", "kernel-format value contains a javascript-format node reference" );
  1217. return object;
  1218. } else {
  1219. return object;
  1220. }
  1221. } );
  1222. }
  1223. /// Determine if a value is a `model/javascript` node.
  1224. ///
  1225. /// This function must run as a method of the driver. Invoke it as:
  1226. /// `valueIsNode.call( driver, value )`.
  1227. ///
  1228. /// @param {Object} value
  1229. ///
  1230. /// @returns {Boolean}
  1231. static valueIsNode( value ) {
  1232. return this.protoNode && // our proxy for the node.vwf prototype
  1233. ( this.protoNode.isPrototypeOf( value ) || value === this.protoNode );
  1234. }
  1235. }
  1236. /// The `application/javascript` media type for scripts that this driver recognizes.
  1237. ///
  1238. /// @field
  1239. VWFJavaScript.scriptMediaType = "application/javascript";
  1240. /// Regex to crack a `Function.toString()` result.
  1241. ///
  1242. /// @field
  1243. VWFJavaScript.functionRegex = new RegExp(
  1244. "function" + // `function`
  1245. "\\s*" +
  1246. "([a-zA-Z_$][0-9a-zA-Z_$]*)?" + // optional name; capture #1
  1247. "\\s*" +
  1248. "\\(([^)]*)\\)" + // `(...)`; capture #2 inside `()`
  1249. "\\s*" +
  1250. "\\{([^]*)\\}" // `{...}`; capture #3 inside `{}`
  1251. );
  1252. VWFJavaScript.kutility = new KUtility();
  1253. VWFJavaScript.utility = new Utility();
  1254. export { VWFJavaScript as default }