Skip to content

Commit

Permalink
JavaScript: Include int/json JSON module as standard
Browse files Browse the repository at this point in the history
  • Loading branch information
markwj committed Jan 17, 2019
1 parent 3441078 commit b9130f8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vehicle/OVMS.V3/components/ovms_script/component.mk
Expand Up @@ -12,5 +12,5 @@ COMPONENT_ADD_INCLUDEDIRS := src
COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive

ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
COMPONENT_EMBED_FILES := jsmodembed/pubsub.js
COMPONENT_EMBED_FILES := jsmodembed/pubsub.js jsmodembed/json.js
endif
1 change: 1 addition & 0 deletions vehicle/OVMS.V3/components/ovms_script/jsmodembed/json.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions vehicle/OVMS.V3/components/ovms_script/jsmodsrc/json.js
@@ -0,0 +1,48 @@
/*
* Module : JSON (not compatible with the browser component!)
* State : test/demo
* Install as : /store/scripts/lib/JSON.js
* Load : JSON = require("lib/JSON");
* Use : JSON.print(object);
*/

exports.print = function(obj, ind)
{
var type = typeof obj;
if (type == "object" && Array.isArray(obj)) type = "array";
if (!ind) ind = '';

switch (type)
{
case "string":
print('"' + obj.replace(/\"/g, '\\\"') + '"');
break;
case "array":
print('[\n');
for (var i = 0; i < obj.length; i++)
{
print(ind + ' ');
exports.print(obj[i], ind + ' ');
if (i != obj.length-1) print(',');
print('\n');
}
print(ind + ']');
break;
case "object":
print('{\n');
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++)
{
print(ind + ' "' + keys[i] + '": ');
exports.print(obj[keys[i]], ind + ' ');
if (i != keys.length-1) print(',');
print('\n');
}
print(ind + '}');
break;
default:
print(obj);
}

if (ind == '') print('\n');
}
17 changes: 17 additions & 0 deletions vehicle/OVMS.V3/components/ovms_script/src/ovms_script.cpp
Expand Up @@ -434,6 +434,16 @@ static duk_ret_t DukOvmsLoadModule(duk_context *ctx)
duk_push_lstring(ctx, mod_pubsub_js_start, length);
return 1;
}
else if (strcmp(module_id,"int/json.js")==0)
{
// Load from internal RAM
extern const char mod_json_js_start[] asm("_binary_json_js_start");
extern const char mod_json_js_end[] asm("_binary_json_js_end");
duk_size_t length = mod_json_js_end - mod_json_js_start;
ESP_LOGD(TAG,"load_cb: id:'%s' (internally provided %d bytes)", module_id, length);
duk_push_lstring(ctx, mod_json_js_start, length);
return 1;
}

ESP_LOGD(TAG,"load_cb: id:'%s', filename:'%s'", module_id, filename);

Expand Down Expand Up @@ -633,6 +643,13 @@ void OvmsScripts::DukTapeInit()
ESP_LOGE(TAG,"Duktape: %s",duk_safe_to_string(m_dukctx, -1));
}
duk_pop(m_dukctx);
ESP_LOGI(TAG,"Duktape: Preload internal module JSON");
duk_push_string(m_dukctx, "(function(){JSON=require(\"int/json\");})();");
if (duk_peval(m_dukctx) != 0)
{
ESP_LOGE(TAG,"Duktape: %s",duk_safe_to_string(m_dukctx, -1));
}
duk_pop(m_dukctx);

// ovmsmain
FILE* sf = fopen("/store/scripts/ovmsmain.js", "r");
Expand Down

0 comments on commit b9130f8

Please sign in to comment.