Skip to content
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

Merged
merged 8 commits into from
Dec 15, 2021

Conversation

mikee47
Copy link
Contributor

@mikee47 mikee47 commented Dec 14, 2021

Add Jerryscript type classes and function support

C++ wrapper classes deal with object lifetime management
Simplifies code, try to make usage as easy and natural as possible
Avoid use of raw C API in applications provides some isolation from upstream changes.
Classes are inline (mostly) so should add little to no overhead to jerryscript API.

External functions can be implemented using classes.
This is enabled using macros to declare/define wrapper functions which check parameter count
and thunk raw jerryscript values into their class equivalents (with zero overhead).

Don't disable float64 support

Floating point numbers don't work at all and 32-bit integers aren't handled correctly.
This is because natural integer storage in ECMA is limited:

  • ECMA_INTEGER_NUMBER_MAX = 0x7ffffff = 134217727
  • ECMA_INTEGER_NUMBER_MIN = -0x7fffff = -8388607

Larger values are stored as floating-point.

Sort out jerryscript debugging

Always abort on jerryscript exception (no point in calling exit)
Enable assertions and messages in debug builds
Emit log messages to flash

Add test application

CI runs this with both minimal/es.next profiles and in debug/release builds.
Focus is on correct code operation and ensuring absence of memory leaks in jerryscript heap.

Always abort on jerryscript exception (no point in calling exit)
Enable assertions and messages in debug builds
Emit log messages to flash
Eliminating use of raw C API provides some isolation from upstream changes
Natural integer storage in ECMA is limited:
ECMA_INTEGER_NUMBER_MAX = 0x7ffffff  = 134217727
ECMA_INTEGER_NUMBER_MIN = -0x7fffff  = -8388607

With float64 disabled, standard integer numbers outside this range aren't represented accurately.
Floating point generally seems broken without it as well.
@slaff slaff merged commit 1b30f27 into slaff:develop Dec 15, 2021
@slaff
Copy link
Owner

slaff commented Dec 15, 2021

@mikee47 Fantastic PR! I really enjoy your work!

Copy link
Contributor Author

@mikee47 mikee47 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slaff Just some extra thoughts, in particular the VirtualMachine class seems un-necessary. Let me know what you think , can submit another PR if you agree?

@@ -4,29 +4,30 @@
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* Jsvm.h
* VirtualMachine.h
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we really need a VirtualMachine class at all since it contains no state. We could instead place these methods as regular functions.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know what you think , can submit another PR if you agree?
.. We could instead place these methods as regular functions.

Ok, let's have it this way.

{
static const char* null_str = "\\u0000";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why the original implementation of this bothered display NULL differently, can't think of any particular reason (other than debugging I guess). It complicated the code so just removed it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

Comment on lines +34 to +43
define BuildAndRun
$(MAKE) snap-clean && MODULE="$1.$3" $(MAKE) JERRY_COMPACT_PROFILE=$2 SMING_RELEASE=$4 run
endef

.PHONY: execute
execute:
$(call BuildAndRun,minimal,1,debug,0)
$(call BuildAndRun,minimal,1,release,1)
$(call BuildAndRun,es.next,0,debug,0)
$(call BuildAndRun,es.next,0,release,1)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Names don't appear correctly in appveyor! We just get 'minimal.debug' twice... Investigating...

Comment on lines +16 to +26
/**
* @brief Function to register event listeners
* @code {.javascript}
*
* event = {
* name => "TEMP_CHANGE",
* params = {
* },
* "origin" => "The\Creator\Of\The\Event"
* };
*
Copy link
Contributor Author

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?

Copy link
Owner

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 or cpu/temp and extern/temp1.

Comment on lines -19 to +86
jsVm.registerFunction("addEventListener", addEventListener);
vm.registerFunction("addEventListener", addEventListener);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, this could translate into JS::global().registerFunction(...)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have it as suggested.

Comment on lines -21 to +88
if(!jsVm.load(main_snap)) {
if(!vm.load(main_snap)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS::loadSnapshot(...)

Copy link
Owner

@slaff slaff Dec 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

Comment on lines -27 to +94
if(!jsVm.runFunction("init")) {
if(!vm.runFunction("init")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS::global().runFunction(...)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed too.

@mikee47 mikee47 deleted the feature/types branch December 15, 2021 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants