mirror of https://github.com/svaarala/duktape.git
Browse Source
A lot of changes to add preliminary lightfunc support: * Add LIGHTFUNC tagged type to duk_tval.h and API. * Internal changes for preliminary to support lightfuncs in call handling and other operations (FIXMEs left in obvious places where support is still missing after this commit) * Preliminary Ecmascript and API testcases for lightfuncs Detailed notes: * Because magic is signed, reading it back involves sign extension which is quite verbose to do in C. Use macros for reading the magic value and other bit fields encoded in the flags. * Function.prototype.bind(): the 'length' property of a bound function now comes out wrong. We could simply look up the virtual 'length' property even if h_target is NULL: no extra code and binding is relatively rare in hot paths. Rewrite more cleanly in any case. * The use flag DUK_USE_LIGHTFUNC_BUILTINS controls the forced lightfunc conversion of built-ins. This results in non-compliant built-ins but significant memory savings in very memory poor environments. * Reject eval(), Thread.yield/resume as lightfuncs. These functions have current assertions that they must be called as fully fledged functions. * Lightfuncs are serialized like ordinary functions for JSON, JX, and JC by this diff. * Add 'magic' to activation for lightfuncs. It will be needed for lightweight functions: we don't have the duk_tval related to the lightfunc, so we must copy the magic value to the activation when a call is made. * When lightfuncs are used as property lookup base values, continue property lookup from the Function.prototype object. This is necessary to allow e.g. ``func.call()`` and ``func.apply()`` to be used. * Call handling had to be reworked for lightfuncs, especially how bound function chains are handled. This is a relatively large change but is necessary to support lightweight functions properly in bound function resolution. The current solution is not ideal. The bytecode executor will first try an ecma-to-ecma call setup which resolves the bound function chain first. If the final, unbound function is not viable (a native function) the call setup returns with an error code. The caller will then perform a normal call. Although bound function resolution has already been done, the normal call handling code will re-do it (and detect there is nothing to do). This situation could be avoided by decoupling bound function handling and effective this binding computation from the actual call setup. The caller could then to do this prestep first, and only then decide whether to use an ecma-to-ecma call or an ordinary heavyweight call. Remove duk__find_nonbound_function as unused. * Use indirect magic to allow LIGHTFUNCs for Date. Most of the built-in functions not directly eligible as lightfuncs are the Date built-in methods, whose magic values contain too much information to fit into the 8-bit magic of a LIGHTFUNC value. To work around this, add an array (duk__date_magics[]) containing the actual control flags needed by the built-ins, and make the Date built-in magic value an index into this table. With this change Date built-ins are successfully converted to lightfuncs. Testcase fixes: - Whitespace fixes - Print error for indirect eval error to make diagnosis easier - Fix error string to match errmsg updated in this branchpull/85/head
Sami Vaarala
11 years ago
40 changed files with 2539 additions and 449 deletions
@ -0,0 +1,76 @@ |
|||
/*
|
|||
* Behavior of lightweight functions in various situations. |
|||
* |
|||
* Also documents the detailed behavior and limitations of lightfuncs. |
|||
*/ |
|||
|
|||
/*===
|
|||
FIXME |
|||
still here |
|||
===*/ |
|||
|
|||
/* FIXME: test all arg counts and lengths, including varargs */ |
|||
|
|||
/* FIXME: duk_to_object() coercion, stack policy */ |
|||
|
|||
/* FIXME: duk_push_current_function() for a lightfunc, check magic, perhaps length */ |
|||
|
|||
static duk_ret_t test_magic(duk_context *ctx) { |
|||
/* FIXME */ |
|||
/* magic limits, C api test, check what happens if you exceed */ |
|||
return 0; |
|||
} |
|||
|
|||
static duk_ret_t test_enum(duk_context *ctx) { |
|||
/* FIXME: push lightfunc here instead of relying on a built-in */ |
|||
duk_eval_string(ctx, "Math.max"); |
|||
|
|||
printf("enum defaults\n"); |
|||
duk_enum(ctx, -1, 0); |
|||
while (duk_next(ctx, -1, 0 /*get_value*/)) { |
|||
printf("key: %s\n", duk_to_string(ctx, -1)); |
|||
duk_pop(ctx); |
|||
} |
|||
duk_pop(ctx); |
|||
printf("top: %ld\n", (long) duk_get_top(ctx)); |
|||
|
|||
printf("enum nonenumerable\n"); |
|||
duk_enum(ctx, -1, DUK_ENUM_INCLUDE_NONENUMERABLE); |
|||
while (duk_next(ctx, -1, 0 /*get_value*/)) { |
|||
printf("key: %s\n", duk_to_string(ctx, -1)); |
|||
duk_pop(ctx); |
|||
} |
|||
duk_pop(ctx); |
|||
printf("top: %ld\n", (long) duk_get_top(ctx)); |
|||
|
|||
printf("enum own\n"); |
|||
duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY); |
|||
while (duk_next(ctx, -1, 0 /*get_value*/)) { |
|||
printf("key: %s\n", duk_to_string(ctx, -1)); |
|||
duk_pop(ctx); |
|||
} |
|||
duk_pop(ctx); |
|||
printf("top: %ld\n", (long) duk_get_top(ctx)); |
|||
|
|||
printf("enum own non-enumerable\n"); |
|||
duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY | DUK_ENUM_INCLUDE_NONENUMERABLE); |
|||
while (duk_next(ctx, -1, 0 /*get_value*/)) { |
|||
printf("key: %s\n", duk_to_string(ctx, -1)); |
|||
duk_pop(ctx); |
|||
} |
|||
duk_pop(ctx); |
|||
printf("top: %ld\n", (long) duk_get_top(ctx)); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
void test(duk_context *ctx) { |
|||
/* nargs / length limits, C api test, check what happens if you exceed */ |
|||
/* Example of using lightfunc as a constructor, separate testcase, doc ref */ |
|||
|
|||
TEST_SAFE_CALL(test_magic); |
|||
TEST_SAFE_CALL(test_enum); |
|||
|
|||
printf("still here\n"); |
|||
fflush(stdout); |
|||
} |
@ -0,0 +1,44 @@ |
|||
/*
|
|||
* If a function modifies its own 'magic' value while it is executing, |
|||
* the change is visible immediately. |
|||
* |
|||
* This is not necessarily stable behavior so user code should not rely |
|||
* on this. The current behavior is a side effect of the way the current |
|||
* call stack state keeping is implemented. This test case just documents |
|||
* the current behavior. |
|||
*/ |
|||
|
|||
/*===
|
|||
*** test_1 (duk_safe_call) |
|||
current magic (on entry): 345 |
|||
current magic (after set): 456 |
|||
final top: 1 |
|||
==> rc=0, result='undefined' |
|||
===*/ |
|||
|
|||
static duk_ret_t my_func(duk_context *ctx) { |
|||
printf("current magic (on entry): %ld\n", (long) duk_get_current_magic(ctx)); |
|||
|
|||
duk_push_current_function(ctx); |
|||
duk_set_magic(ctx, -1, 456); |
|||
|
|||
printf("current magic (after set): %ld\n", (long) duk_get_current_magic(ctx)); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static duk_ret_t test_1(duk_context *ctx) { |
|||
duk_push_c_function(ctx, my_func, 2 /*nargs*/); |
|||
duk_set_magic(ctx, -1, 345); |
|||
|
|||
duk_push_int(ctx, 123); |
|||
duk_push_int(ctx, 234); |
|||
duk_call(ctx, 2); |
|||
|
|||
printf("final top: %ld\n", (long) duk_get_top(ctx)); |
|||
return 0; |
|||
} |
|||
|
|||
void test(duk_context *ctx) { |
|||
TEST_SAFE_CALL(test_1); |
|||
} |
@ -0,0 +1,60 @@ |
|||
/* |
|||
* JSON, JX, and JC serialization of lightfuncs. |
|||
*/ |
|||
|
|||
/*--- |
|||
{ |
|||
"custom": true, |
|||
"specialoptions": "DUK_OPT_LIGHTFUNC_BUILTINS" |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
undefined undefined |
|||
string [1,2,3,null,4,5,6] |
|||
string {"foo":123} |
|||
string {_func:true} |
|||
string [1,2,3,{_func:true},4,5,6] |
|||
string {foo:123,bar:{_func:true}} |
|||
string {"_func":true} |
|||
string [1,2,3,{"_func":true},4,5,6] |
|||
string {"foo":123,"bar":{"_func":true}} |
|||
===*/ |
|||
|
|||
function test() { |
|||
// FIXME: rely on Math.cos being a lightfunc
|
|||
var lf = Math.cos; |
|||
|
|||
function json(x) { |
|||
var res = JSON.stringify(x); |
|||
print(typeof res, res); |
|||
} |
|||
|
|||
function jx(x) { |
|||
var res = Duktape.enc('jx', x); |
|||
print(typeof res, res); |
|||
} |
|||
|
|||
function jc(x) { |
|||
var res = Duktape.enc('jc', x); |
|||
print(typeof res, res); |
|||
} |
|||
|
|||
json(lf); |
|||
json([ 1, 2, 3, lf, 4, 5, 6 ]); |
|||
json({ foo: 123, bar: lf }); |
|||
|
|||
jx(lf); |
|||
jx([ 1, 2, 3, lf, 4, 5, 6 ]); |
|||
jx({ foo: 123, bar: lf }); |
|||
|
|||
jc(lf); |
|||
jc([ 1, 2, 3, lf, 4, 5, 6 ]); |
|||
jc({ foo: 123, bar: lf }); |
|||
} |
|||
|
|||
try { |
|||
test(); |
|||
} catch (e) { |
|||
print(e); |
|||
} |
File diff suppressed because it is too large
Loading…
Reference in new issue