<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Works like a charm :)<br>
<br>
I've added the autoinit option to the web UI.<br>
<br>
Important for all script writers is to keep in mind we now have a
global shared context.<br>
<br>
That is very useful to share data between scripts, but needs a
little bit of discipline to avoid polluting the global context with
local variables and functions. Any "var", "const", "function" etc.
placed into the top level of a script file now is added to the
global context.<br>
<br>
The best approach is to encourage users to use closures by default
for all their scripts, i.e.<br>
<blockquote><font size="-1"><tt>(function(){<br>
… user code …<br>
</tt></font><font size="-1"><tt>})();</tt></font><br>
</blockquote>
The same rule applies to web plugins.<br>
<br>
Node style modules also work nicely. A simple test module:<br>
<blockquote><tt><font size="-1">// Module : JSON (not
compatible with the browser component!)<br>
// State : test/demo<br>
// Install as : /store/scripts/lib/JSON.js<br>
// Load : JSON = require("lib/JSON");<br>
// Use : JSON.print(object);<br>
<br>
exports.print = function(obj, ind) {<br>
var type = typeof obj;<br>
if (type == "object" && Array.isArray(obj)) type =
"array";<br>
if (!ind) ind = '';<br>
<br>
switch (type) {<br>
case "string":<br>
print('"' + obj.replace(/\"/g, '\\\"') + '"');<br>
break;<br>
case "array":<br>
print('[\n');<br>
for (var i = 0; i < obj.length; i++) {<br>
print(ind + ' ');<br>
exports.print(obj[i], ind + ' ');<br>
if (i != obj.length-1) print(',');<br>
print('\n');<br>
}<br>
print(ind + ']');<br>
break;<br>
case "object":<br>
print('{\n');<br>
var keys = Object.keys(obj);<br>
for (var i = 0; i < keys.length; i++) {<br>
print(ind + ' "' + keys[i] + '": ');<br>
exports.print(obj[keys[i]], ind + ' ');<br>
if (i != keys.length-1) print(',');<br>
print('\n');<br>
}<br>
print(ind + '}');<br>
break;<br>
default:<br>
print(obj);<br>
}<br>
<br>
if (ind == '') print('\n');<br>
}</font></tt><br>
</blockquote>
<br>
Using it:<br>
<blockquote><font size="-1"><tt>JSON = require("lib/JSON");<br>
<br>
print("Global context:\n");<br>
JSON.print(this);<br>
<br>
(function(){<br>
var x = { a: 42, b: "a \"foo\" is no 'bar'", c: [1,2,3], d:
{ sub: true }, e: ["q","w",17, { e1:22, e2:33 }] };<br>
print("\nLocal object:\n");<br>
JSON.print(x);<br>
})();</tt></font><br>
</blockquote>
<br>
Execution:<br>
<blockquote><tt>OVMS# . test.js</tt><tt><br>
</tt><tt>Global context:</tt><tt><br>
</tt><tt>{</tt><tt><br>
</tt><tt> "print": function () { [native code] },</tt><tt><br>
</tt><tt> "assert": function () { [native code] },</tt><tt><br>
</tt><tt> "OvmsMetricValue": function () { [native code] },</tt><tt><br>
</tt><tt> "OvmsMetricFloat": function () { [native code] },</tt><tt><br>
</tt><tt> "OvmsLocationStatus": function () { [native code] },</tt><tt><br>
</tt><tt> "OvmsCommand": function () { [native code] },</tt><tt><br>
</tt><tt> "JSON": {</tt><tt><br>
</tt><tt> "print": function () { [ecmascript code] }</tt><tt><br>
</tt><tt> }</tt><tt><br>
</tt><tt>}</tt><tt><br>
</tt><tt><br>
</tt><tt>Local object:</tt><tt><br>
</tt><tt>{</tt><tt><br>
</tt><tt> "a": 42,</tt><tt><br>
</tt><tt> "b": "a \"foo\" is no 'bar'",</tt><tt><br>
</tt><tt> "c": [</tt><tt><br>
</tt><tt> 1,</tt><tt><br>
</tt><tt> 2,</tt><tt><br>
</tt><tt> 3</tt><tt><br>
</tt><tt> ],</tt><tt><br>
</tt><tt> "d": {</tt><tt><br>
</tt><tt> "sub": true</tt><tt><br>
</tt><tt> },</tt><tt><br>
</tt><tt> "e": [</tt><tt><br>
</tt><tt> "q",</tt><tt><br>
</tt><tt> "w",</tt><tt><br>
</tt><tt> 17,</tt><tt><br>
</tt><tt> {</tt><tt><br>
</tt><tt> "e1": 22,</tt><tt><br>
</tt><tt> "e2": 33</tt><tt><br>
</tt><tt> }</tt><tt><br>
</tt><tt> ]</tt><tt><br>
</tt><tt>}</tt><tt><br>
</tt><br>
</blockquote>
Nice :)<br>
<br>
Regards,<br>
Michael<br>
<br>
<br>
<div class="moz-cite-prefix">Am 10.01.19 um 10:10 schrieb Mark
Webb-Johnson:<br>
</div>
<blockquote type="cite"
cite="mid:DF73994D-9A32-4391-AFC7-EE157683A77B@webb-johnson.net">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
I’ve committed my work on a production javascript framework. The
changes are quite extensive, and with the one exception of
‘ovmsprint’, should be backwards compatible.
<div class=""><br class="">
</div>
<div class="">The overall goal is to be able to run a javascript
code system alongside our standard firmware code. Users could
then write javascript, and modify scripts already running on the
device (without firmware changes). Unlike the previous version
(where scripts were loaded, compiled, run, then dumped), this
approach is designed to keep scripts in memory; this vastly
increases the opportunities for what can be done with the
system.</div>
<div class=""><br class="">
</div>
<div class="">Changes made include:<br class="">
<div class=""><br class="">
</div>
<div class="">
<div class="">
<ul class="MailOutline">
<li class="">Move scripting to be a component</li>
<li class="">Make DukTape use SPIRAM for as much as
possible</li>
<li class="">Run the javascript engine in it's own task
(OVMS DukTape)</li>
<li class="">Change the way extensions functions are
registered<br class="">
(just call RegisterDuktapeFunction, rather than messing
around with the internals of Duktape)</li>
<li class="">Catch compilation and parsing errors (fail
gracefully - finally!)</li>
<li class="">Support 'print' and 'assert' javascript
framework</li>
<li class="">Output (via print) goes to current console,
or logged if no console</li>
<li class="">Autoinit (and run) /store/scripts/ovmsmain.js</li>
<li class="">Support node.js style modules</li>
<li class="">Control with config auto javascript (default
enable)</li>
</ul>
</div>
<div class=""><br class="">
</div>
<div class="">The main TODO is to provide a facility for
reloading the engine (new scripts/modules). At the moment,
the module needs to be rebooted (ugly). There is also a lot
of work still to do on making some actually useful modules.
And we also need to work out what to do with events (now
that they can be delivered to running javascripts).</div>
<div class=""><br class="">
</div>
<div class="">But, anyway, for the moment:</div>
</div>
<div class=""><br class="">
</div>
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding:
0px;" class="">
<div class="">
<div class="">
<div class=""><font class="" face="Andale Mono"><span
style="font-size: 18px;" class="">I (128) script:
Initialising SCRIPTS (1600)</span></font></div>
<div class=""><font class="" face="Andale Mono"><span
style="font-size: 18px;" class="">I (132) script:
Using DUKTAPE javascript engine</span></font></div>
</div>
</div>
<div class="">
<div class=""><font class="" face="Andale Mono"><span
style="font-size: 18px;" class="">I (0) script: Duktape:
Creating heap</span></font></div>
<div class=""><font class="" face="Andale Mono"><span
style="font-size: 18px;" class="">I (10) script:
Duktape: Initialising module system</span></font></div>
<div class=""><font class="" face="Andale Mono"><span
style="font-size: 18px;" class="">I (20) script:
Duktape: Scripting task is running</span></font></div>
</div>
<div class=""><font class="" face="Andale Mono"><span
style="font-size: 18px;" class="">I (906) script: Duktape:
Executing ovmsmain.js</span></font></div>
<div class=""><font class="" face="Andale Mono"><span
style="font-size: 18px;" class="">I (936) script: Hello
world</span></font></div>
<div class=""><font class="" face="Andale Mono"><span
style="font-size: 18px;" class=""><br class="">
</span></font></div>
<div class="">
<div class=""><font class="" face="Andale Mono"><span
style="font-size: 18px;" class="">OVMS# vfs cat
/store/scripts/ovmsmain.js</span></font></div>
<div class=""><font class="" face="Andale Mono"><span
style="font-size: 18px;" class="">print("Hello
world\n”);</span></font></div>
</div>
<div class=""><font class="" face="Andale Mono"><span
style="font-size: 18px;" class=""><br class="">
</span></font></div>
<div class=""><font class="" face="Andale Mono"><span
style="font-size: 18px;" class="">OVMS# test javascript<br
class="">
Javascript 1+2=3</span></font></div>
</blockquote>
<div class="">
<div class=""><br class="">
</div>
<div class="">Regards, Mark.<br class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On 8 Jan 2019, at 11:00 PM, Michael Balzer
<<a href="mailto:dexter@expeedo.de" class=""
moz-do-not-send="true">dexter@expeedo.de</a>>
wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="">Mark,<br class="">
<br class="">
welcome back, awesome news on the JS and FCC stuff :)<br
class="">
<br class="">
I've got some fixes and some feedback on the web
plugins in progress, I'll get that done until the
weekend (got some free days left).<br class="">
<br class="">
Regards,<br class="">
Michael<br class="">
<br class="">
<br class="">
Am 08.01.19 um 05:27 schrieb Mark Webb-Johnson:<br
class="">
<blockquote type="cite" class="">Firstly, a very
belated merry xmas, and happy 2019 to everyone. I
hope that you all got to spend good times with
friends and family, as I did. I’ve been travelling,
and just got back home to a very large eMail inbox.<br
class="">
<br class="">
During my travels, I did manage to re-work the
javascript framework to be single task based. That
seems to work well, and now I’m back I will test on
my car. The new framework is backwards compatible -
apart from memory usage (one more task with a
stack), it has no impact to the flow. It will,
however, allow us to extend this a lot more and
become much more useful. Assuming no issues, I will
be able to commit that soon.<br class="">
<br class="">
As we are up to 144 commits since 3.1.011, perhaps
it is well past due for 3.1.012? I was hoping that
Espressif would have fixed the bug related to wear
levelling version upgrades that Michael helped to
identify, but not yet. Any objections to a 3.1.012
release to EAP at the end of this week?<br class="">
<br class="">
The CE/FCC certification work is progressing. No
issues so far. I’ll let you know as soon as we get
near completion for this.<br class="">
<br class="">
I’m working through my inbox now, and will reply
individually to questions there.<br class="">
<br class="">
Regards, Mark.<br class="">
<br class="">
_______________________________________________<br
class="">
OvmsDev mailing list<br class="">
<a href="mailto:OvmsDev@lists.openvehicles.com"
class="" moz-do-not-send="true">OvmsDev@lists.openvehicles.com</a><br
class="">
<a class="moz-txt-link-freetext" href="http://lists.openvehicles.com/mailman/listinfo/ovmsdev">http://lists.openvehicles.com/mailman/listinfo/ovmsdev</a><br class="">
</blockquote>
<br class="">
-- <br class="">
Michael Balzer * Helkenberger Weg 9 * D-58256
Ennepetal<br class="">
Fon 02333 / 833 5735 * Handy 0176 / 206 989 26<br
class="">
<br class="">
<br class="">
_______________________________________________<br
class="">
OvmsDev mailing list<br class="">
<a href="mailto:OvmsDev@lists.openvehicles.com"
class="" moz-do-not-send="true">OvmsDev@lists.openvehicles.com</a><br
class="">
<a class="moz-txt-link-freetext" href="http://lists.openvehicles.com/mailman/listinfo/ovmsdev">http://lists.openvehicles.com/mailman/listinfo/ovmsdev</a><br
class="">
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
OvmsDev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:OvmsDev@lists.openvehicles.com">OvmsDev@lists.openvehicles.com</a>
<a class="moz-txt-link-freetext" href="http://lists.openvehicles.com/mailman/listinfo/ovmsdev">http://lists.openvehicles.com/mailman/listinfo/ovmsdev</a>
</pre>
</blockquote>
<br>
<pre class="moz-signature" cols="160">--
Michael Balzer * Helkenberger Weg 9 * D-58256 Ennepetal
Fon 02333 / 833 5735 * Handy 0176 / 206 989 26
</pre>
</body>
</html>