-
Notifications
You must be signed in to change notification settings - Fork 682
Common but non-standard JS function implementations in jerry-ext #1787
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
Common but non-standard JS function implementations in jerry-ext #1787
Conversation
bd3bd97
to
8f51423
Compare
From all extensions, this should land first because it is simple, and we can define the extension format here. |
jerry-ext/extfunc/extfunc-assert.c
Outdated
* limitations under the License. | ||
*/ | ||
|
||
#include "jerryscript-ext/extfunc.h" |
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.
I don't remember what was the conclusion, shall we use ext or util?
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.
I just realized that ext can mean extension or extra, and here it sounds like extra.
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 'ext' in 'jerryscript-ext' stands for 'extension' (and there, we've concluded to use 'ext', not 'util'). The 'ext' in 'extfunc' stands for 'external', as in 'jerry_create_external_function' (and there, we haven't concluded what would be the best name -- I've copied this over from the original PR as I had to give some name to the jerry-ext module).
jerry-ext/extfunc/extfunc-assert.c
Outdated
* limitations under the License. | ||
*/ | ||
|
||
#include "jerryscript-ext/extfunc.h" |
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.
I just realized that ext can mean extension or extra, and here it sounds like extra.
void | ||
jerryx_port_extfunc_print_char (char c) /**< the character to print */ | ||
{ | ||
printf ("%c", c); |
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.
I think this function is unnecessary.
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.
Note, that without this function, the 'print' handler does not work. (It cannot have printf
hardwired because we have platforms (targets) where printf
is unavailable.)
The alternative is to hardwire printf
into the print handler, but then we will lose reusability, as many targets will have to copy-paste the code, which we wanted to avoid AFAIK.
Having said that, I'm not sure that this is the best approach. And I'm definitely open to suggestions.
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.
What about jerry_port_console?
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.
We are just removing that in #1749 .
Moreover, the needs of the external print handler are a bit different from the print implementation that we had as a builtin. (The builtin expected a printf-like vararg interface from jerry_port_console, but the current external print handler needs to write a single char only -- which IMHO is good, as vararg turned out to be a pain sometimes and for some targets.)
Moreover 2, jerry_port_console suggests that all ports implement it, but that's not the case. If an app does not use jerry-ext, it will not need to implement jerry_port_console.
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.
Ok.
* Common external function handlers | ||
*/ | ||
|
||
jerry_value_t jerryx_extfunc_assert (const jerry_value_t func_obj_val, const jerry_value_t this_p, |
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.
I don't really like this prefix+prefix notation. I think we have enough names, and there will be no clashes, so this can simply be jerryx_assert
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.
My argument for prefixing: jerry-ext is like multiple libraries merged into one. As if we had one library for argument validation, one for common external function handlers, etc. They all should have their own "namespaces". They already have separate headers (following the pattern "jerryscript-ext/NAMESPACE.h"). IMHO, they should also have their own identifier namespaces: "jerryx_NAMESPACE_whatever".
*/ | ||
|
||
#ifndef JERRYX_EXTFUNC_H | ||
#define JERRYX_EXTFUNC_H |
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.
As for me jerry-ext/include/jerryscript-ext/extfunc.h is too long. I would prefer:
jerry-ext/include/jerryscript-extfunc.h
or
jerry-ext/jerryscript-ext/extfunc.h
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.
This is just source tree organization. It will/should not be reflected in the include directives. Basically, "jerry-ext/include" is on the include path and users will still have to use #include "jerryscript-ext/extfunc.h"
(or, as another example, #include "jerryscript-ext/args.h"
for the argument validator) only in the sources. And having that "include" path component under "jerry-ext" serves two causes: 1) eases header installation with cmake, and 2) consistency (all our libraries follow that sheme -- except for "jerry-core" but I'm trying to fix that up in #1793).
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.
Ok keep it that way but please to find a better name for extfunc.h which describe its 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.
What about "jerryscript-ext/external.h" for header and jerryx_external_xxx
for identifiers? That's just one character longer than extfunc?
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.
Ok for header name. But jerryx_external_ is jerry-external-external
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.
Nope. jerry-extension-external. Much better :)
267f346
to
77d14a4
Compare
I've updated the PR. The "extfunc" module name got replaced by "external" all over the code. The PR still does not contain the "infrastructure" (build system) of jerry-ext. IMHO, It would not be fair to rip it off from #1740. |
* value marked with error flag - otherwise | ||
*/ | ||
jerry_value_t | ||
jerryx_external_register_global_function (const jerry_char_t *name_p, /**< name of the function */ |
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.
I feel a bit redundancy in this kind of naming. I thought x
in jerryx is for external
. I think external is unnecessary, but this is only my personal preference. I'm not sure was there a final consensus in the naming or not.
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.
We should really write this down at some point of time so that we remember what is what in our chosen naming scheme. In #1740, when jerry-ext vs jerry-util was disputed (for the name of the library), we have decided for ext, to stand for extension (#1740 (comment), #1740 (comment), #1740 (comment)). And identifiers of the jerry-ext library to have jerryx_ prefix (where x still stands for extension).
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.
Yes it is confusing, it can be extension, external, extra.
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.
Maybe jerry-ext/util/global-func.c
?
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.
@akosthekiss I can deal with the current name, but what about jerryx_builtin_
as @zherczeg suggested (#1787 (review)) I think it would be better.
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.
I'm against "builtin": builtin is something that's built in. These functions are not. They are external. The term "external builtin" is self-contradictory.
But I'm also against polluting the "global namespace" of jerry-ext with each and every function. We should keep the concept of "modules" or "sub-libraries" within jerry-ext. The currently proposed one is to deal with (common) external functions.
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.
To me jerryscript-ext/assert.h
sounds like a file which defines a macro similar to JERRY_ASSERT
rather than providing a callback for a JS function.
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.
This gonna be a long debate on the prefix... Just one more thing to add to the mix: let's not forget that we are using jerry_create_external_function
and jerry_external_handler_t
to create external functions. I thought that this was an established phrase for functions we bound into the JS scope.
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.
Still LGTM from my side. You may land with the current prefixes if you want.
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.
To me jerryscript-ext/assert.h sounds like a file which defines a macro similar to JERRY_ASSERT rather than providing a callback for a JS function.
Hmm jerryscript-ext/js-assert.h
?
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.
LGTM
@zherczeg and @akosthekiss I think these two comments are kinds of conflicting:
I think @jiangzidong 's PR (#1740) is very close to being done + it includes the build system stuff for jerry-ext. @zherczeg OK to land that first? |
77d14a4
to
669d454
Compare
Ok land that first. |
669d454
to
7be9b79
Compare
Now that #1740 has landed, I've rebased this PR on latest master and made use of the jerry-ext infrastructure. On top of the previous commit, there is now also the adaptation of jerry-main to use the implementation of the common JS functions from jerry-ext. |
7be9b79
to
2fc41a3
Compare
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.
I ma still not happy wit the name of extension, although it is literally correct. In other places of the project these functions are called built-ins, and external builtins sounds better than external extensions. But the patch is ok, and I can live with this name.
New idea: what about esfunc-assert.h? So we emphasize that it is an ECMAScript function. |
I just noticed that there is no documentation for the new API. |
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.
Documentation is missing.
@zherczeg To me, esfunc is no better than extfunc that was downvoted originally. And I still don't see why we would need separate headers for each external function handler (we don't have multiple headers for all the argument validation and transform functions). |
I've added documentation for the extension. |
jerry-ext/external/external-print.c
Outdated
#include "jerryscript-ext/external.h" | ||
|
||
|
||
/** |
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.
Only one newline.
jerry-ext/external/external-gc.c
Outdated
* @return undefined. | ||
*/ | ||
jerry_value_t | ||
jerryx_external_gc (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */ |
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 __attribute__((unused))
is gcc specific, we should use JERRY_UNUSED()
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.
We don't have access to that here, but I'll rewrite it to (void) (func_obj_val);
The difference is that |
What about |
For the sake of accuracy, these functions are called "external function handler"s in jerry-core, and their type is As for "jerryx_print_handler": I'll strongly resist against anything unprefixed. Right now, we have arg in jerry-ext, then we have this PR, and contexts are also coming, and who knows what's next. I'll keep insisting to have each "module" within its own "identifier namespace" (+ directory, + header). Would |
Probably |
I'll rename everything to jerryx_handler_ then, if noone objects. Not sure about the plural in the header name. Argument handling extension uses singular, context extension likewise. |
9ecfb52
to
e2ba767
Compare
I've applied the name change, it's in a commit on top of the rest (will squash if everyone is happy with it) |
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.
LGTM
Added `handler` module to `jerry-ext` to contain implementation of commonly used external function handlers: `assert`, `gc`, and `print`. Also adapted jerry-main to use jerry-ext/handler JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
e2ba767
to
f116008
Compare
This PR splits out the implementation of "common but non-standard" functions
from PR #1749. The goal is to save various targets from re-implementing external
function handlers for
print
,gc
, orassert
. The idea is to provide thesehandlers as a module of the now-forming
jerry-ext
library.The PR is still Work in Progress, because
there is a debate whether all function handlers should be in one module (i.e.,
source sub-directory) of
jerry-ext
, or each should go into its own subdir,there is no consensus what would be a good name for the module(s),
I'm not sure how
print
could be implemented in the most portable but alsoefficient way (because a reusable implementation cannot rely on libc's
printf
-- but then each port/app must somehow provide its own C-levelprinting routine, which we are just getting rid of in Remove the built-in print function #1749...), and
jerry-ext
is not in master yet, so there is no build system, CMakeLists,etc. in this PR.
Note: we can also conclude that such an approach as presented here actually does not add much to reusability, because ports will not share these implementations, e.g., because they have seriously different needs for
assert
orprint
. Please, give your feedback, maintainers oftargets
are especially summoned.