Browse Source

typing rework

pull/9/head
Sami Vaarala 11 years ago
parent
commit
de9151c384
  1. 2
      src/duk_api.c
  2. 20
      src/duk_api_public.h
  3. 32
      src/duk_error.h
  4. 2
      src/duk_error_augment.c
  5. 28
      src/duk_error_macros.c
  6. 4
      src/duk_error_misc.c
  7. 8
      src/duk_error_throw.c
  8. 3
      src/duk_features.h

2
src/duk_api.c

@ -16,7 +16,7 @@
#ifndef DUK_USE_VARIADIC_MACROS
const char *duk_api_global_filename = NULL;
int duk_api_global_line = 0;
duk_int_t duk_api_global_line = 0;
#endif
/*

20
src/duk_api_public.h

@ -21,6 +21,10 @@ extern "C" {
*
* (duk_context *) maps directly to internal type (duk_hthread *).
* Currently only primitive typedefs have a '_t' suffix.
*
* Many types are wrapped by Duktape for portability to rare platforms
* where e.g. 'int' is a 16-bit type. See typing discussion in Duktape
* web guide.
*/
/* Index values must have at least 32-bit range. */
@ -44,10 +48,10 @@ typedef duk_ret_t (*duk_c_function)(duk_context *ctx);
typedef void *(*duk_alloc_function) (void *udata, duk_size_t size);
typedef void *(*duk_realloc_function) (void *udata, void *ptr, duk_size_t size);
typedef void (*duk_free_function) (void *udata, void *ptr);
typedef void (*duk_fatal_function) (duk_context *ctx, int code, const char *msg);
typedef void (*duk_decode_char_function) (void *udata, int codepoint);
typedef int (*duk_map_char_function) (void *udata, int codepoint);
typedef int (*duk_safe_call_function) (duk_context *ctx);
typedef void (*duk_fatal_function) (duk_context *ctx, duk_errcode_t code, const char *msg);
typedef void (*duk_decode_char_function) (void *udata, duk_codepoint_t codepoint);
typedef duk_codepoint_t (*duk_map_char_function) (void *udata, duk_codepoint_t codepoint);
typedef duk_ret_t (*duk_safe_call_function) (duk_context *ctx);
struct duk_memory_functions {
duk_alloc_function alloc;
@ -59,12 +63,12 @@ struct duk_memory_functions {
struct duk_function_list_entry {
const char *key;
duk_c_function value;
int nargs;
duk_int_t nargs;
};
struct duk_number_list_entry {
const char *key;
double value;
duk_double_t value;
};
/*
@ -88,7 +92,7 @@ struct duk_number_list_entry {
/* Indicates that a native function does not have a fixed number of args,
* and the argument stack should not be capped/extended at all.
*/
#define DUK_VARARGS (-1)
#define DUK_VARARGS ((duk_int_t) (-1))
/* Number of value stack entries (in addition to actual call arguments)
* guaranteed to be allocated on entry to a Duktape/C function.
@ -195,7 +199,7 @@ struct duk_number_list_entry {
#ifndef DUK_API_VARIADIC_MACROS
extern const char *duk_api_global_filename;
extern int duk_api_global_line;
extern duk_int_t duk_api_global_line;
#endif
/*

32
src/duk_error.h

@ -50,7 +50,7 @@
#ifdef DUK_USE_VARIADIC_MACROS
/* __VA_ARGS__ has comma issues for empty lists, so we mandate at least 1 argument for '...' (format string) */
#define DUK_ERROR(thr,err,...) duk_err_handle_error(DUK_FILE_MACRO, (int) DUK_LINE_MACRO, (thr), (err), __VA_ARGS__)
#define DUK_ERROR(thr,err,...) duk_err_handle_error(DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (thr), (err), __VA_ARGS__)
#define DUK_ERROR_RAW(file,line,thr,err,...) duk_err_handle_error((file), (line), (thr), (err), __VA_ARGS__)
#else /* DUK_USE_VARIADIC_MACROS */
@ -61,7 +61,7 @@
#define DUK_ERROR \
duk_err_file_stash = (const char *) DUK_FILE_MACRO, \
duk_err_line_stash = (int) DUK_LINE_MACRO, \
duk_err_line_stash = (duk_int_t) DUK_LINE_MACRO, \
(void) duk_err_handle_error_stash /* arguments follow */
#define DUK_ERROR_RAW duk_err_handle_error
@ -203,32 +203,32 @@
#ifdef DUK_USE_VERBOSE_ERRORS
#ifdef DUK_USE_VARIADIC_MACROS
DUK_NORETURN(void duk_err_handle_error(const char *filename, int line, duk_hthread *thr, int code, const char *fmt, ...));
DUK_NORETURN(void duk_err_handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...));
#else /* DUK_USE_VARIADIC_MACROS */
extern const char *duk_err_file_stash;
extern int duk_err_line_stash;
DUK_NORETURN(void duk_err_handle_error(const char *filename, int line, duk_hthread *thr, int code, const char *fmt, ...));
DUK_NORETURN(void duk_err_handle_error_stash(duk_hthread *thr, int code, const char *fmt, ...));
extern duk_int_t duk_err_line_stash;
DUK_NORETURN(void duk_err_handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...));
DUK_NORETURN(void duk_err_handle_error_stash(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...));
#endif /* DUK_USE_VARIADIC_MACROS */
#else /* DUK_USE_VERBOSE_ERRORS */
#ifdef DUK_USE_VARIADIC_MACROS
DUK_NORETURN(void duk_err_handle_error(duk_hthread *thr, int code));
DUK_NORETURN(void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code));
#else /* DUK_USE_VARIADIC_MACROS */
DUK_NORETURN(void duk_err_handle_error_nonverbose1(duk_hthread *thr, int code, const char *fmt, ...));
DUK_NORETURN(void duk_err_handle_error_nonverbose2(const char *filename, int line, duk_hthread *thr, int code, const char *fmt, ...));
DUK_NORETURN(void duk_err_handle_error_nonverbose1(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...));
DUK_NORETURN(void duk_err_handle_error_nonverbose2(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...));
#endif /* DUK_USE_VARIADIC_MACROS */
#endif /* DUK_USE_VERBOSE_ERRORS */
#ifdef DUK_USE_VERBOSE_ERRORS
DUK_NORETURN(void duk_err_create_and_throw(duk_hthread *thr, duk_uint32_t code, const char *msg, const char *filename, int line));
DUK_NORETURN(void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code, const char *msg, const char *filename, duk_int_t line));
#else
DUK_NORETURN(void duk_err_create_and_throw(duk_hthread *thr, duk_uint32_t code));
DUK_NORETURN(void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code));
#endif
DUK_NORETURN(void duk_error_throw_from_negative_rc(duk_hthread *thr, int rc));
DUK_NORETURN(void duk_error_throw_from_negative_rc(duk_hthread *thr, duk_ret_t rc));
#if defined(DUK_USE_AUGMENT_ERROR_CREATE)
void duk_err_augment_error_create(duk_hthread *thr, duk_hthread *thr_callstack, const char *filename, int line, int noblame_fileline);
void duk_err_augment_error_create(duk_hthread *thr, duk_hthread *thr_callstack, const char *filename, duk_int_t line, int noblame_fileline);
#endif
#if defined(DUK_USE_AUGMENT_ERROR_THROW)
void duk_err_augment_error_throw(duk_hthread *thr);
@ -236,15 +236,15 @@ void duk_err_augment_error_throw(duk_hthread *thr);
DUK_NORETURN(void duk_err_longjmp(duk_hthread *thr));
DUK_NORETURN(void duk_default_fatal_handler(duk_context *ctx, int code, const char *msg));
DUK_NORETURN(void duk_default_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg));
#if !defined(DUK_USE_PANIC_HANDLER)
DUK_NORETURN(void duk_default_panic_handler(int code, const char *msg));
DUK_NORETURN(void duk_default_panic_handler(duk_errcode_t code, const char *msg));
#endif
void duk_err_setup_heap_ljstate(duk_hthread *thr, int lj_type);
duk_hobject *duk_error_prototype_from_code(duk_hthread *thr, int err_code);
duk_hobject *duk_error_prototype_from_code(duk_hthread *thr, duk_errcode_t err_code);
#endif /* DUK_ERROR_H_INCLUDED */

2
src/duk_error_augment.c

@ -389,7 +389,7 @@ static void duk__err_augment_builtin_throw(duk_hthread *thr, duk_hthread *thr_ca
*/
#if defined(DUK_USE_AUGMENT_ERROR_CREATE)
void duk_err_augment_error_create(duk_hthread *thr, duk_hthread *thr_callstack, const char *filename, int line, int noblame_fileline) {
void duk_err_augment_error_create(duk_hthread *thr, duk_hthread *thr_callstack, const char *filename, duk_int_t line, int noblame_fileline) {
duk_context *ctx = (duk_context *) thr;
duk_hobject *obj;

28
src/duk_error_macros.c

@ -9,7 +9,7 @@
#ifdef DUK_USE_VERBOSE_ERRORS
#ifdef DUK_USE_VARIADIC_MACROS
void duk_err_handle_error(const char *filename, int line, duk_hthread *thr, int code, const char *fmt, ...) {
void duk_err_handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
va_list ap;
char msg[DUK__ERRFMT_BUFSIZE];
va_start(ap, fmt);
@ -20,25 +20,25 @@ void duk_err_handle_error(const char *filename, int line, duk_hthread *thr, int
}
#else /* DUK_USE_VARIADIC_MACROS */
const char *duk_err_file_stash = NULL;
int duk_err_line_stash = 0;
duk_int_t duk_err_line_stash = 0;
DUK_NORETURN(static void duk__handle_error(const char *filename, int line, duk_hthread *thr, int code, const char *fmt, va_list ap));
DUK_NORETURN(static void duk__handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, va_list ap));
static void duk__handle_error(const char *filename, int line, duk_hthread *thr, int code, const char *fmt, va_list ap) {
static void duk__handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, va_list ap) {
char msg[DUK__ERRFMT_BUFSIZE];
(void) DUK_VSNPRINTF(msg, sizeof(msg), fmt, ap);
msg[sizeof(msg) - 1] = (char) 0;
duk_err_create_and_throw(thr, code, msg, filename, line);
}
void duk_err_handle_error(const char *filename, int line, duk_hthread *thr, int code, const char *fmt, ...) {
void duk_err_handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
duk__handle_error(filename, line, thr, code, fmt, ap);
va_end(ap); /* dead code */
}
void duk_err_handle_error_stash(duk_hthread *thr, int code, const char *fmt, ...) {
void duk_err_handle_error_stash(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
duk__handle_error(duk_err_file_stash, duk_err_line_stash, thr, code, fmt, ap);
@ -49,16 +49,16 @@ void duk_err_handle_error_stash(duk_hthread *thr, int code, const char *fmt, ...
#else /* DUK_USE_VERBOSE_ERRORS */
#ifdef DUK_USE_VARIADIC_MACROS
void duk_err_handle_error(duk_hthread *thr, int code) {
void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code) {
duk_err_create_and_throw(thr, code);
}
#else /* DUK_USE_VARIADIC_MACROS */
void duk_err_handle_error_nonverbose1(duk_hthread *thr, int code, const char *fmt, ...) {
void duk_err_handle_error_nonverbose1(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
duk_err_create_and_throw(thr, code);
}
void duk_err_handle_error_nonverbose2(const char *filename, int line, duk_hthread *thr, int code, const char *fmt, ...) {
void duk_err_handle_error_nonverbose2(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
duk_err_create_and_throw(thr, code);
}
#endif /* DUK_USE_VARIADIC_MACROS */
@ -69,15 +69,15 @@ void duk_err_handle_error_nonverbose2(const char *filename, int line, duk_hthrea
* Default fatal error handler
*/
void duk_default_fatal_handler(duk_context *ctx, int code, const char *msg) {
void duk_default_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg) {
DUK_UNREF(ctx);
#ifdef DUK_USE_FILE_IO
DUK_FPRINTF(DUK_STDERR, "FATAL %d: %s\n", code, msg ? msg : "null");
DUK_FPRINTF(DUK_STDERR, "FATAL %d: %s\n", (int) code, msg ? msg : "null"); /* FIXME: format */
DUK_FFLUSH(DUK_STDERR);
#else
/* omit print */
#endif
DUK_D(DUK_DPRINT("default fatal handler called, code %d -> calling DUK_PANIC()", code));
DUK_D(DUK_DPRINT("default fatal handler called, code %d -> calling DUK_PANIC()", (int) code));
DUK_PANIC(code, msg);
DUK_UNREACHABLE();
}
@ -87,7 +87,7 @@ void duk_default_fatal_handler(duk_context *ctx, int code, const char *msg) {
*/
#if !defined(DUK_USE_PANIC_HANDLER)
void duk_default_panic_handler(int code, const char *msg) {
void duk_default_panic_handler(duk_errcode_t code, const char *msg) {
#ifdef DUK_USE_FILE_IO
DUK_FPRINTF(DUK_STDERR, "PANIC %d: %s ("
#if defined(DUK_USE_PANIC_ABORT)
@ -99,7 +99,7 @@ void duk_default_panic_handler(int code, const char *msg) {
#else
#error no DUK_USE_PANIC_xxx macro defined
#endif
")\n", code, msg ? msg : "null");
")\n", (int) code, msg ? msg : "null"); /* FIXME: format */
DUK_FFLUSH(DUK_STDERR);
#else
/* omit print */

4
src/duk_error_misc.c

@ -8,8 +8,8 @@
* Get prototype object for an integer error code.
*/
duk_hobject *duk_error_prototype_from_code(duk_hthread *thr, int err_code) {
switch (err_code) {
duk_hobject *duk_error_prototype_from_code(duk_hthread *thr, duk_errcode_t code) {
switch (code) {
case DUK_ERR_EVAL_ERROR:
return thr->builtins[DUK_BIDX_EVAL_ERROR_PROTOTYPE];
case DUK_ERR_RANGE_ERROR:

8
src/duk_error_throw.c

@ -21,18 +21,18 @@
*/
#ifdef DUK_USE_VERBOSE_ERRORS
void duk_err_create_and_throw(duk_hthread *thr, duk_uint32_t code, const char *msg, const char *filename, int line) {
void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code, const char *msg, const char *filename, duk_int_t line) {
#else
void duk_err_create_and_throw(duk_hthread *thr, duk_uint32_t code) {
void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code) {
#endif
duk_context *ctx = (duk_context *) thr;
int double_error = thr->heap->handling_error;
#ifdef DUK_USE_VERBOSE_ERRORS
DUK_DD(DUK_DDPRINT("duk_err_create_and_throw(): code=%d, msg=%s, filename=%s, line=%d",
code, msg ? msg : "null", filename ? filename : "null", line));
(int) code, msg ? msg : "null", filename ? filename : "null", (int) line));
#else
DUK_DD(DUK_DDPRINT("duk_err_create_and_throw(): code=%d", code));
DUK_DD(DUK_DDPRINT("duk_err_create_and_throw(): code=%d", (int) code));
#endif
DUK_ASSERT(thr != NULL);

3
src/duk_features.h

@ -786,6 +786,9 @@ typedef unsigned int duk_small_uint_t;
/* Boolean values are represented with the platform 'int'. */
typedef duk_small_int_t duk_bool_t;
/* Error codes are represented with platform int. */
typedef duk_small_int_t duk_errcode_t;
/* Codepoint type. Must be 32 bits or more because it is used also for
* internal codepoints. The type is signed because negative codepoints
* are used as internal markers (e.g. to mark EOF or missing argument).

Loading…
Cancel
Save