Browse Source

Merge branch 'code-policy-nonascii'

v1.0-maintenance
Sami Vaarala 10 years ago
parent
commit
20ddb99c0f
  1. 2
      api-testcases/test-bug-push-sprintf-va-copy.c
  2. 2
      api-testcases/test-bug-tailcall-preventyield-assert.c
  3. 54
      api-testcases/test-get-lstring.c
  4. 16
      api-testcases/test-get-set-finalizer.c
  5. 116
      api-testcases/test-get-string.c
  6. 20
      src/duk_bi_global.c
  7. 2
      src/duk_hobject_props.c
  8. 18
      util/check_code_policy.py

2
api-testcases/test-bug-push-sprintf-va-copy.c

@ -3,7 +3,7 @@
* and the initial buffer size was not enough. This bug testcase tries to
* trigger that issue. Unfortunately it is not triggered with every compiler.
*
* Reported by Andreas Öman.
* Reported by Andreas Oman.
*/
/*===

2
api-testcases/test-bug-tailcall-preventyield-assert.c

@ -1,6 +1,6 @@
/*
* In Duktape 0.10.0 the following assert would fail in some cases (reported
* by Andreas Öman):
* by Andreas Oman):
*
* assertion failed: (act->flags & DUK_ACT_FLAG_PREVENT_YIELD) == 0 (duk_js_call.c:1914)
*

54
api-testcases/test-get-lstring.c

@ -1,54 +0,0 @@
/*===
top: 10
index 0: string '(null)', length 0
index 0: string '(null)'
index 1: string '(null)', length 0
index 1: string '(null)'
index 2: string '(null)', length 0
index 2: string '(null)'
index 3: string '(null)', length 0
index 3: string '(null)'
index 4: string '', length 0
index 4: string ''
index 5: string 'foo', length 3
index 5: string 'foo'
index 6: string 'foo', length 7
index 6: string 'foo'
index 7: string 'xyz', length 6
index 7: string 'xyz'
index 8: string '(null)', length 0
index 8: string '(null)'
index 9: string '(null)', length 0
index 9: string '(null)'
===*/
void test(duk_context *ctx) {
duk_idx_t i, n;
duk_push_undefined(ctx);
duk_push_null(ctx);
duk_push_true(ctx);
duk_push_false(ctx);
duk_push_string(ctx, "");
duk_push_string(ctx, "foo");
duk_push_lstring(ctx, "foo\0bar", 7);
duk_push_string(ctx, "\xe1\x88\xb4xyz"); /* 4 chars, first char utf-8 encoded U+1234 */
duk_push_nan(ctx);
duk_push_object(ctx);
n = duk_get_top(ctx);
printf("top: %ld\n", (long) n);
for (i = 0; i < n; i++) {
const char *buf;
size_t len;
len = (size_t) 0xdeadbeef;
buf = duk_get_lstring(ctx, i, &len);
printf("index %ld: string '%s', length %lu\n",
(long) i, buf, (unsigned long) len);
buf = duk_get_lstring(ctx, i, NULL);
printf("index %ld: string '%s'\n", (long) i, buf);
}
}

16
api-testcases/test-get-set-finalizer.c

@ -47,7 +47,7 @@ static duk_ret_t test_basic(duk_context *ctx) {
duk_eval_string(ctx, "(function() { return 'target object'; })");
duk_put_prop_string(ctx, -2, "toString");
/* [ target ] */
/* [ target ] */
/* Set finalizer */
duk_push_c_function(ctx, basic_finalizer, 1);
@ -55,7 +55,7 @@ static duk_ret_t test_basic(duk_context *ctx) {
duk_put_prop_string(ctx, -2, "myName");
duk_set_finalizer(ctx, -2);
/* [ target ] */
/* [ target ] */
printf("top: %ld\n", (long) duk_get_top(ctx));
@ -67,7 +67,7 @@ static duk_ret_t test_basic(duk_context *ctx) {
printf("top: %ld\n", (long) duk_get_top(ctx));
/* [ target ] */
/* [ target ] */
printf("before set top 0\n");
duk_set_top(ctx, 0);
@ -115,12 +115,12 @@ static duk_ret_t test_recursive_finalizer(duk_context *ctx) {
duk_put_prop_string(ctx, -2, "quux");
duk_set_finalizer(ctx, -2);
/* [ target(foo:123) finalizer(bar:321) ] */
/* [ target(foo:123) finalizer(bar:321) ] */
/* Set Ecmascript finalizer to original object */
duk_set_finalizer(ctx, -2);
/* [ target(foo:123) ] */
/* [ target(foo:123) ] */
printf("top: %ld\n", (long) duk_get_top(ctx));
@ -128,7 +128,7 @@ static duk_ret_t test_recursive_finalizer(duk_context *ctx) {
duk_get_finalizer(ctx, -1); /* target's finalizer */
duk_get_finalizer(ctx, -1); /* finalizer's finalizer */
/* [ target(foo:123) finalizer(bar:321) c_finalizer(quux:234) ] */
/* [ target(foo:123) finalizer(bar:321) c_finalizer(quux:234) ] */
duk_get_prop_string(ctx, -2, "bar");
printf("finalizer.bar=%s\n", duk_safe_to_string(ctx, -1));
@ -137,13 +137,13 @@ static duk_ret_t test_recursive_finalizer(duk_context *ctx) {
printf("c_finalizer.quux=%s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
/* [ target(foo:123) finalizer(bar:321) c_finalizer(quux:234) ] */
/* [ target(foo:123) finalizer(bar:321) c_finalizer(quux:234) ] */
printf("before set top 0\n");
duk_set_top(ctx, 0); /* causes finalization */
printf("after set top 0\n");
/* [ ] */
/* [ ] */
/* Explicit GC (just in case e.g. a reference loop prevented collection) */
printf("before explicit gc\n");

116
api-testcases/test-get-string.c

@ -1,18 +1,42 @@
static void dump(const unsigned char *buf) {
const unsigned char *p = buf;
int first = 1;
if (!buf) {
printf("null\n");
return;
}
printf("[");
while (*p) {
if (first) {
first = 0;
} else {
printf(" ");
}
printf("%02x", (int) (*p));
p++;
}
printf("]\n");
}
/*===
*** test_get_string (duk_safe_call)
top: 10
index 0: string '(null)'
index 1: string '(null)'
index 2: string '(null)'
index 3: string '(null)'
index 4: string ''
index 5: string 'foo'
index 6: string 'foo'
index 7: string 'xyz'
index 8: string '(null)'
index 9: string '(null)'
index 0: null
index 1: null
index 2: null
index 3: null
index 4: []
index 5: [66 6f 6f]
index 6: [66 6f 6f]
index 7: [e1 88 b4 78 79 7a]
index 8: null
index 9: null
==> rc=0, result='undefined'
===*/
void test(duk_context *ctx) {
static duk_ret_t test_get_string(duk_context *ctx) {
duk_idx_t i, n;
duk_push_undefined(ctx);
@ -29,6 +53,74 @@ void test(duk_context *ctx) {
n = duk_get_top(ctx);
printf("top: %ld\n", (long) n);
for (i = 0; i < n; i++) {
printf("index %ld: string '%s'\n", (long) i, duk_get_string(ctx, i));
printf("index %ld: ", (long) i);
dump((const unsigned char *) duk_get_string(ctx, i));
}
return 0;
}
/*===
*** test_get_lstring (duk_safe_call)
top: 10
index 0: length 0: null
index 0: null
index 1: length 0: null
index 1: null
index 2: length 0: null
index 2: null
index 3: length 0: null
index 3: null
index 4: length 0: []
index 4: []
index 5: length 3: [66 6f 6f]
index 5: [66 6f 6f]
index 6: length 7: [66 6f 6f]
index 6: [66 6f 6f]
index 7: length 6: [e1 88 b4 78 79 7a]
index 7: [e1 88 b4 78 79 7a]
index 8: length 0: null
index 8: null
index 9: length 0: null
index 9: null
==> rc=0, result='undefined'
===*/
static duk_ret_t test_get_lstring(duk_context *ctx) {
duk_idx_t i, n;
duk_push_undefined(ctx);
duk_push_null(ctx);
duk_push_true(ctx);
duk_push_false(ctx);
duk_push_string(ctx, "");
duk_push_string(ctx, "foo");
duk_push_lstring(ctx, "foo\0bar", 7);
duk_push_string(ctx, "\xe1\x88\xb4xyz"); /* 4 chars, first char utf-8 encoded U+1234 */
duk_push_nan(ctx);
duk_push_object(ctx);
n = duk_get_top(ctx);
printf("top: %ld\n", (long) n);
for (i = 0; i < n; i++) {
const char *buf;
size_t len;
len = (size_t) 0xdeadbeef;
buf = duk_get_lstring(ctx, i, &len);
printf("index %ld: length %lu: ",
(long) i, (unsigned long) len);
dump((const unsigned char *) buf);
buf = duk_get_lstring(ctx, i, NULL);
printf("index %ld: ", (long) i);
dump((const unsigned char *) buf);
}
return 0;
}
void test(duk_context *ctx) {
TEST_SAFE_CALL(test_get_string);
TEST_SAFE_CALL(test_get_lstring);
}

20
src/duk_bi_global.c

@ -422,7 +422,7 @@ duk_ret_t duk_bi_global_object_eval(duk_context *ctx) {
return 1; /* return arg as-is */
}
/* [ source ] */
/* [ source ] */
comp_flags = DUK_JS_COMPILE_FLAG_EVAL;
act_eval = thr->callstack + thr->callstack_top - 1; /* this function */
@ -453,7 +453,7 @@ duk_ret_t duk_bi_global_object_eval(duk_context *ctx) {
DUK_ASSERT(func != NULL);
DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) func));
/* [ source template ] */
/* [ source template ] */
/* E5 Section 10.4.2 */
DUK_ASSERT(thr->callstack_top >= 1);
@ -525,7 +525,7 @@ duk_ret_t duk_bi_global_object_eval(duk_context *ctx) {
duk_js_push_closure(thr, func, outer_var_env, outer_lex_env);
/* [ source template closure ] */
/* [ source template closure ] */
if (this_to_global) {
DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
@ -544,11 +544,11 @@ duk_ret_t duk_bi_global_object_eval(duk_context *ctx) {
(duk_heaphdr *) outer_var_env,
(duk_tval *) duk_get_tval(ctx, -1)));
/* [ source template closure this ] */
/* [ source template closure this ] */
duk_call_method(ctx, 0);
/* [ source template result ] */
/* [ source template result ] */
return 1;
}
@ -987,7 +987,7 @@ duk_ret_t duk_bi_global_object_require(duk_context *ctx) {
return 1;
}
/* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined ] */
/* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined ] */
DUK_ASSERT_TOP(ctx, 7);
/*
@ -1024,7 +1024,7 @@ duk_ret_t duk_bi_global_object_require(duk_context *ctx) {
duk_dup(ctx, 3); /* resolved id: require(id) must return this same module */
duk_def_prop_stridx(ctx, 9, DUK_STRIDX_ID, DUK_PROPDESC_FLAGS_NONE);
/* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module ] */
/* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module ] */
DUK_ASSERT_TOP(ctx, 10);
/*
@ -1099,7 +1099,7 @@ duk_ret_t duk_bi_global_object_require(duk_context *ctx) {
* Call the wrapped module function.
*/
/* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module mod_func ] */
/* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module mod_func ] */
DUK_ASSERT_TOP(ctx, 11);
duk_dup(ctx, 8); /* exports (this binding) */
@ -1107,12 +1107,12 @@ duk_ret_t duk_bi_global_object_require(duk_context *ctx) {
duk_dup(ctx, 8); /* exports (argument) */
duk_dup(ctx, 9); /* module (argument) */
/* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module mod_func exports fresh_require exports module ] */
/* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module mod_func exports fresh_require exports module ] */
DUK_ASSERT_TOP(ctx, 15);
duk_call_method(ctx, 3 /*nargs*/);
/* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module result(ignored) ] */
/* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module result(ignored) ] */
DUK_ASSERT_TOP(ctx, 11);
duk_pop_2(ctx);

2
src/duk_hobject_props.c

@ -3118,7 +3118,7 @@ duk_bool_t duk_hobject_putprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_
duk_dup(ctx, -4);
duk_call_method(ctx, 2); /* [key setter this val key] -> [key retval] */
#else
duk_call_method(ctx, 1); /* [key setter this val] -> [key retval] */
duk_call_method(ctx, 1); /* [key setter this val] -> [key retval] */
#endif
duk_pop(ctx); /* ignore retval -> [key] */
goto success_no_arguments_exotic;

18
util/check_code_policy.py

@ -29,6 +29,7 @@ re_debuglog_callsite = re.compile(r'^.*?(DUK_D+PRINT).*?$')
re_trailing_ws = re.compile(r'^.*?\s$')
re_only_ws = re.compile(r'^\s*$')
re_identifier = re.compile(r'[A-Za-z0-9_]+')
re_nonascii = re.compile(r'^.*?[\x80-\xff].*?$')
# These identifiers are wrapped in duk_features.h.in, and should only be used
# through the wrappers elsewhere.
@ -226,6 +227,7 @@ def checkFixme(line, filename):
raise Exception('FIXME on line')
def checkIdentifiers(line, filename):
# XXX: this now executes for every line which is pointless
bn = os.path.basename(filename)
excludePlain = (bn == 'duk_features.h.in' or \
bn[0:5] == 'test-')
@ -235,6 +237,16 @@ def checkIdentifiers(line, filename):
if not excludePlain:
raise Exception('invalid identifier %r (perhaps plain)' % m.group(0))
def checkNonAscii(line, filename):
m = re_nonascii.match(line)
if m is not None:
bn = os.path.basename(filename)
if bn == 'test-lex-utf8.js':
# this specific file is intentionally exempt
pass
else:
raise Exception('non-ascii character')
def processFile(filename, checkersRaw, checkersNoComments, checkersNoExpectStrings):
f = open(filename, 'rb')
dataRaw = f.read()
@ -276,6 +288,9 @@ def processFile(filename, checkersRaw, checkersNoComments, checkersNoExpectStrin
if len(linesRaw) > 2 and linesRaw[0] == '':
problems.append(Problem(filename, 1, '(no line)', 'First line is empty'))
def asciiOnly(x):
return re.sub(r'[\x80-\xff]', '#', x)
def main():
parser = optparse.OptionParser()
parser.add_option('--dump-vim-commands', dest='dump_vim_commands', default=False, help='Dump oneline vim command')
@ -285,6 +300,7 @@ def main():
checkersRaw.append(checkDebugLogCalls)
checkersRaw.append(checkCarriageReturns)
checkersRaw.append(checkFixme)
checkersRaw.append(checkNonAscii)
checkersNoComments = []
checkersNoComments.append(checkIdentifiers)
@ -304,7 +320,7 @@ def main():
tmp += ' ' + str(i.filename) + ' : ' + str(i.reason)
while len(tmp) < 80:
tmp = tmp + ' '
tmp += ' - ' + i.line.strip()
tmp += ' - ' + asciiOnly(i.line.strip())
print(tmp)
print '*** Total: %d problems' % len(problems)

Loading…
Cancel
Save