-
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add class support and function wrappers #7
Changes from all commits
a6cf6d1
73979f9
4812d9a
8ea078c
44898e3
b09f061
633e472
9913945
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,109 @@ | ||
#include <SmingCore.h> | ||
#include <Jsvm.h> | ||
#include <vm_functions.h> | ||
#include <Jerryscript.h> | ||
|
||
namespace | ||
{ | ||
|
||
Jsvm jsVm; | ||
Timer tempTimer; | ||
JS::VirtualMachine vm; | ||
SimpleTimer timer; | ||
HashMap<String, JS::Callable::List> events; | ||
|
||
IMPORT_FSTR(main_snap, PROJECT_DIR "/out/jerryscript/main.js.snap") | ||
|
||
namespace JS | ||
{ | ||
using namespace Jerryscript; | ||
|
||
/** | ||
* @brief Function to register event listeners | ||
* @code {.javascript} | ||
* | ||
* event = { | ||
* name => "TEMP_CHANGE", | ||
* params = { | ||
* }, | ||
* "origin" => "The\Creator\Of\The\Event" | ||
* }; | ||
* | ||
* addEventListener("TEMP_CHANGE", function(event) { | ||
* console.log("Got Event" + event.name); | ||
* }); | ||
* | ||
* @endcode | ||
* | ||
*/ | ||
Value addEventListener(const CallInfo& callInfo, Value& eventName, Callable& function) | ||
{ | ||
if(!eventName.isString() || !function.isCallable()) { | ||
return ArgumentError(__FUNCTION__); | ||
} | ||
|
||
events[eventName].add(function); | ||
return true; | ||
} | ||
|
||
} // namespace JS | ||
|
||
JS_DEFINE_FUNCTION(addEventListener, 2) | ||
|
||
/* | ||
* Dispatch an event to all registered listeners | ||
* | ||
* Event = { | ||
* name: "eventName" | ||
* params: { | ||
* "property1": value1, | ||
* "property2": value2, | ||
* ... | ||
* } | ||
* } | ||
*/ | ||
bool triggerEvent(const String& name, const JS::Object& params) | ||
{ | ||
if(!events.contains(name)) { | ||
debug_e("%s: Unknown event '%s'", __FUNCTION__, name.c_str()); | ||
return false; | ||
} | ||
|
||
// Build the event object... | ||
JS::Object event; | ||
event["name"] = name; | ||
event["params"] = params; | ||
|
||
auto realm = JS::global(); | ||
for(auto& listener : events[name]) { | ||
listener.call(realm, event); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void startJsvm() | ||
{ | ||
/* | ||
* This is how we register a new function in JavaScript | ||
* that will communicate directly with our C/C++ code. | ||
*/ | ||
jsVm.registerFunction("addEventListener", addEventListener); | ||
vm.registerFunction("addEventListener", addEventListener); | ||
Comment on lines
-19
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, this could translate into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's have it as suggested. |
||
|
||
if(!jsVm.load(main_snap)) { | ||
if(!vm.load(main_snap)) { | ||
Comment on lines
-21
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. |
||
debug_e("Failed to load snapshot"); | ||
return; | ||
} | ||
|
||
// Now you can initialize your script by calling the init() JavaScript function | ||
if(!jsVm.runFunction("init")) { | ||
if(!vm.runFunction("init")) { | ||
Comment on lines
-27
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed too. |
||
debug_e("Failed executing the init function."); | ||
} | ||
|
||
/* | ||
* Here we trigger every 2 seconds an event inside the JavaScript code. | ||
*/ | ||
tempTimer.initializeMs(2000, TimerDelegate([](){ | ||
JsEventData params; | ||
params["temp"]="20"; | ||
timer.initializeMs<2000>([]() { | ||
JS::Object params; | ||
params["temp"] = 20; | ||
triggerEvent("EVENT_TEMP", params); | ||
})).start(); | ||
}); | ||
timer.start(); | ||
} | ||
|
||
} // namespace | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "origin" property sticks out a bit here. Weird value too! Purpose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There idea is the following: If you have two or more temperature sensors may be you would like to know which one sent the event. And the "origin" property can be used to provide that information. The value of the property is free text so it can be just
temp1
orcpu/temp
andextern/temp1
.