<div><br></div><div dir="auto">If the get proxy doesn’t work after the set proxy is defined, it may be worth defining a get proxy in the same Proxy object.</div><div dir="auto"><br></div><div dir="auto">This was a potential solution from my dreams.</div><div dir="auto"><br></div><div dir="auto">John<br><div class="gmail_quote" dir="auto"><div dir="ltr" class="gmail_attr">On Sun, Oct 18, 2020 at 8:42 AM Andreas Plesch <<a href="mailto:andreasplesch@gmail.com">andreasplesch@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div dir="ltr"><div dir="auto">I added SFRotation as a Proxy to a Quaternion target to get the ScriptEvents example working:<div dir="auto"><br></div><div dir="auto"><a href="https://raw.githack.com/andreasplesch/x3dom/7cc55c8a98ffac62cc273d7b1fb0fa92ac7ebacd/test/regression-suite/test/cases/x3dscript/ScriptEvents.html" target="_blank">https://raw.githack.com/andreasplesch/x3dom/7cc55c8a98ffac62cc273d7b1fb0fa92ac7ebacd/test/regression-suite/test/cases/x3dscript/ScriptEvents.html</a><br></div><div dir="auto"><br></div><div dir="auto">It should be possible to add SFRotation to x3dom.fields in a separate PR independent of X3DScript progress.</div><div dir="auto"><br></div><div>I think it should be possible to use Proxies for output fields of scripts. The targets would be the actual field objects of the script node, and there would be a set trap which invokes sending the event. Worth a try.</div><div><br></div><div>Scripts in Prototype do not work yet. I think it is because the custom script node fields are not available yet when the protoinstance is constructed.</div><div dir="auto"><br></div><div>-Andreas</div><div dir="auto"><br></div><div dir="auto"><div data-smartmail="gmail_signature" dir="auto">---on the phone---</div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Oct 17, 2020, 1:25 PM Andreas Plesch <<a href="mailto:andreasplesch@gmail.com" target="_blank">andreasplesch@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)">I tried another simple x3d script, a simple bouncer, in x_ite:<br>
<br>
<a href="https://gist.githack.com/andreasplesch/8ded7b7ffb598a63c44318f5810b260d/raw/63c673c9bc177c9ad64a3e5a1ad9bd6f7180921a/safe.html" rel="noreferrer noreferrer" target="_blank">https://gist.githack.com/andreasplesch/8ded7b7ffb598a63c44318f5810b260d/raw/63c673c9bc177c9ad64a3e5a1ad9bd6f7180921a/safe.html</a><br>
<a href="https://gist.github.com/andreasplesch/8ded7b7ffb598a63c44318f5810b260d#file-bouncersafe-x3d" rel="noreferrer noreferrer" target="_blank">https://gist.github.com/andreasplesch/8ded7b7ffb598a63c44318f5810b260d#file-bouncersafe-x3d</a><br>
<br>
It required adding initialization of given field values, as variables<br>
in the script. The script node has the fields, with their values, so I<br>
am now passing the script node itself to the function which evaluates<br>
the script. This way the node and its fields become available to the<br>
script. Here is the working example:<br>
<br>
<a href="https://raw.githack.com/andreasplesch/x3dom/695821820e34b8a6571b9fec722337735e632468/test/regression-suite/test/cases/x3dscript/Bouncer.html" rel="noreferrer noreferrer" target="_blank">https://raw.githack.com/andreasplesch/x3dom/695821820e34b8a6571b9fec722337735e632468/test/regression-suite/test/cases/x3dscript/Bouncer.html</a><br>
<br>
In dev tools you can drill down to see X3DScript.js, or look at my<br>
"fields" branch.<br>
<br>
It also turned out that watching for changes in output fields is not<br>
workable and somewhat wrong, so currently just all output fields fire<br>
events each time a script set function (an input event) is called.<br>
Perhaps it is necessary to use a Proxy, or setters which then can fire<br>
events.<br>
<br>
Generally, this would be my approach to scripts, their fields and<br>
routing events. Please feel free to take whatever you find useful.<br>
There would be lots to do, for example, creating a translation between<br>
official SAI functions and x3dom sai like functions. For example<br>
SFVec3f[0] is SFVec3f.x in x3dom. Browser.print is<br>
x3dom.debug.logWarning and so on. The translation could be included in<br>
each script. A more difficult part is to figure out how to deal with<br>
SFNode, MFNode fields.<br>
<br>
With regards to security, it may be worthwhile to just scan the script<br>
for "document" or "window" and refuse to evaluate. But of course this<br>
would not be a real solution. I think adding explicit opt in to allow<br>
execution of scripts somehow may be an option. Perhaps just by<br>
offering a separate version of x3dom, from a less well known location<br>
or even on request.<br>
<br>
Cheers, -Andreas<br>
<br>
On Sat, Oct 17, 2020 at 7:48 AM Andreas Plesch <<a href="mailto:andreasplesch@gmail.com" rel="noreferrer" target="_blank">andreasplesch@gmail.com</a>> wrote:<br>
><br>
> Hi John,<br>
><br>
> since I had started to sketch some answers I went ahead and implemented a rough draft of a X3DScript node for x3dom.<br>
><br>
> Here is the basic spec. example working with the node:<br>
><br>
> <a href="https://raw.githack.com/andreasplesch/x3dom/4cf1ec31736109e67d7b44aa1afb8fc8133f8cb6/test/regression-suite/test/cases/x3dscript/TouchSensorIsOverEventECMAScript.html" rel="noreferrer noreferrer" target="_blank">https://raw.githack.com/andreasplesch/x3dom/4cf1ec31736109e67d7b44aa1afb8fc8133f8cb6/test/regression-suite/test/cases/x3dscript/TouchSensorIsOverEventECMAScript.html</a><br>
><br>
> Here is the implementation:<br>
><br>
> <a href="https://github.com/andreasplesch/x3dom/blob/fields/src/nodes/Scripting/X3DScript.js" rel="noreferrer noreferrer" target="_blank">https://github.com/andreasplesch/x3dom/blob/fields/src/nodes/Scripting/X3DScript.js</a><br>
><br>
> I am using "X3DScript" for now because it is less confusing. It should be possible to switch to Script type='app/x3dscript' at some point.<br>
><br>
> It turns out that in html documents CDATA sections get converted into comments. This is great since it lets one use any character ( except for -->) in the script. So I am looking for converted comments under the script node for the script source.  I did not look into xhtml but xml should be easier.<br>
><br>
> Since MDN also recommends Function over eval, I tried to go this way and could figure out an (unsafe) solution using wrappers to maintain the internal context of the script. I think the same approach may be possible for x_ite as well.<br>
><br>
> I use the nice x_ite approach to get access to the functions in the script, by returning references to the functions.<br>
><br>
> Unlike x_ite I am trying not to use with() since it is not recommended. Instead I add lots of helpers to the script source in a preamble, I think like you do. For output fields x_ite is also using with() and getters and setters. For now, I am trying to avoid with() and just compare output field values before and after a script set function gets called, from a route. If there is a change, an event is fired.<br>
><br>
> There is no attempt to deal with XFNode field values. This will be tricky. I did not test anything other than the simple example but other simple examples may start to work as well, perhaps with some adjustments to how SAI is used. Gears will need SFRotation which may be easy to add to the preamble.<br>
><br>
> Cheers, -Andreasq<br>
> ---on the phone---<br>
><br>
><br>
> On Thu, Oct 15, 2020 at 10:31 PM John Carlson <<a href="mailto:yottzumm@gmail.com" rel="noreferrer" target="_blank">yottzumm@gmail.com</a>> wrote:<br>
> ><br>
> ><br>
> ><br>
> > On Thu, Oct 15, 2020 at 9:25 PM John Carlson <<a href="mailto:yottzumm@gmail.com" rel="noreferrer" target="_blank">yottzumm@gmail.com</a>> wrote:<br>
> >><br>
> >> What I am searching for is:<br>
> >><br>
> >> 1. How to add Script related routes to X3DOM<br>
> ><br>
> ><br>
> > Once I set up Script and fields, this is done automatically with setupTree?<br>
> ><br>
> >><br>
> >> 2. How to hook in Proto IS statements found in Scripts.<br>
> ><br>
> ><br>
> > Not sure how to do that yet.<br>
> >><br>
> >> 3. How to modify any Script code to support parsing and the above.<br>
> ><br>
> ><br>
> > adding any functions, declaring variables, etc.<br>
> >><br>
> >><br>
> >> On Thu, Oct 15, 2020 at 9:15 PM John Carlson <<a href="mailto:yottzumm@gmail.com" rel="noreferrer" target="_blank">yottzumm@gmail.com</a>> wrote:<br>
> >>><br>
> >>> Something like<br>
> >>><br>
> >>> fromNode.setupRoute(fromField, toNode, toField);<br>
> >>><br>
> >>> ? That's only for routes?  What about events?<br>
> >>><br>
> >>> John<br>
> >>><br>
> >>><br>
> >>> On Thu, Oct 15, 2020 at 8:42 PM John Carlson <<a href="mailto:yottzumm@gmail.com" rel="noreferrer" target="_blank">yottzumm@gmail.com</a>> wrote:<br>
> >>>><br>
> >>>><br>
> >>>>>> Use the set_xfield functions on field_changed (xfield).<br>
> >>>>><br>
> >>>>><br>
> >>>>> Not implemented.  Is this new code?   Where would I put it under Scripting/?   Can you provide a short example?<br>
> >>>>>><br>
> >>>>>><br>
> >>>>>> Post messages for all _changed fields as given in the field def.<br>
> >>>>>> Somehow deal with SFNode fields.<br>
> >>>>>><br>
> >>>>>><br>
> >>>><br>
> >>>> There is sample code under Script.js:<br>
> >>>> <a href="https://github.com/coderextreme/X3DJSONLD/blob/master/src/main/node/Script.js#L346" rel="noreferrer noreferrer" target="_blank">https://github.com/coderextreme/X3DJSONLD/blob/master/src/main/node/Script.js#L346</a><br>
> >>>><br>
> >>>> Below:<br>
> >>>> ====================================================================<br>
> >>>>         doRoute(mypackage, fromNode, fromField, toNode, toField, log, set, changed, selector, url) {<br>
> >>>>                 var fromScript = mypackage.find(fromNode);<br>
> >>>>                 var toScript = mypackage.find(toNode);<br>
> >>>>                 // only add routes with scripts involved<br>
> >>>>                 if (typeof fromScript !== 'undefined' || typeof toScript !== 'undefined') {<br>
> >>>>                         var from =                       this.nodeUtil(selector)+fromNode+"','"+fromField+"')";<br>
> >>>>                         if (typeof fromScript !== 'undefined') {<br>
> >>>>                                 from = 'typeof '+this.useX3DJSON('Obj', selector, url, fromScript.name)+'.'+fromField+changed+' === "function" ? '+this.useX3DJSON('Obj', selector, url, fromScript.name) + '.'+fromField+changed+'() : '+this.useX3DJSON('Obj', selector, url, fromScript.name) + '.'+fromField;<br>
> >>>>                         }<br>
> >>>>                         var to =                        this.nodeUtil(selector)+toNode+"','"+toField+"',";<br>
> >>>>                         if (typeof toScript !== 'undefined') {<br>
> >>>>                                 to = this.useX3DJSON('Obj', selector, url, toScript.name) + '.'+set+toField+'(';<br>
> >>>>                         }<br>
> >>>>                         if (typeof fromScript !== 'undefined' && typeof toScript !== 'undefined') {<br>
> >>>>                                 log.log("               if ("+this.useX3DJSON('Obj', selector, url, toScript.name)+" && "+from+") {");<br>
> >>>>                         }<br>
> >>>>                         log.log("                       "+to+from+", __eventTime);");<br>
> >>>>                         if (typeof fromScript !== 'undefined' && typeof toScript !== 'undefined') {<br>
> >>>>                                 log.log("               }");<br>
> >>>>                         }<br>
> >>>>                 }<br>
> >>>>         }<br>
> >>>><br>
> >>>>  ====================================================================<br>
> >>>><br>
><br>
><br>
<br>
<br>
-- <br>
Andreas Plesch<br>
Waltham, MA 02453<br>
</blockquote></div>
</blockquote></div></div>