static duk_ret_t dukky_html_anchor_element_target_getter(duk_context *ctx) { /* Get private data for method */ html_anchor_element_private_t *priv = NULL; duk_push_this(ctx); duk_get_prop_string(ctx, -1, dukky_magic_string_private); priv = duk_get_pointer(ctx, -1); duk_pop_2(ctx); if (priv == NULL) { return 0; /* can do? No can do. */ } #line 34 "HTMLAnchorElement.bnd" dom_exception exc; dom_string *str; exc = dom_html_anchor_element_get_target((struct dom_html_anchor_element *)((node_private_t*)priv)->node, &str); if (exc != DOM_NO_ERR) { return 0; } if (str != NULL) { duk_push_lstring(ctx, dom_string_data(str), dom_string_length(str)); dom_string_unref(str); } else { duk_push_lstring(ctx, NULL, 0); } return 1; }
static duk_ret_t dukzip_unz_getfileinfo(duk_context *ctx) { duk_idx_t info_obj; // unz_file_info fileInfo; unz_file_info64 fileInfo; unzFile archive = dukzip_unz_from_this(ctx); // unzGetCurrentFileInfo(archive, &fileInfo, NULL, 0, NULL, 0, NULL, 0); unzGetCurrentFileInfo64(archive, &fileInfo, NULL, 0, NULL, 0, NULL, 0); #if defined(__GNUC__) && !defined(DUK_NO_VLA) char fileName[fileInfo.size_filename], extraField[fileInfo.size_file_extra], commentString[fileInfo.size_file_comment]; #else char *fileName = malloc(fileInfo.size_filename); char *extraField = malloc(fileInfo.size_file_extra); char *commentString = malloc(fileInfo.size_file_comment); #endif // unzGetCurrentFileInfo(archive, &fileInfo, // fileName, fileInfo.size_filename, // extraField, fileInfo.size_file_extra, // commentString, fileInfo.size_file_comment); unzGetCurrentFileInfo64(archive, &fileInfo, fileName, fileInfo.size_filename, extraField, fileInfo.size_file_extra, commentString, fileInfo.size_file_comment); info_obj = duk_push_object(ctx); duk_push_int(ctx, fileInfo.compressed_size); duk_put_prop_string(ctx, info_obj, "compressed"); duk_push_int(ctx, fileInfo.uncompressed_size); duk_put_prop_string(ctx, info_obj, "uncompressed"); duk_push_lstring(ctx, fileName, fileInfo.size_filename); duk_put_prop_string(ctx, info_obj, "filename"); duk_push_lstring(ctx, extraField, fileInfo.size_file_extra); duk_put_prop_string(ctx, info_obj, "extra"); duk_push_lstring(ctx, commentString, fileInfo.size_file_comment); duk_put_prop_string(ctx, info_obj, "comment"); #if !defined(__GNUC__) || defined(DUK_NO_VLA) free(fileName); free(extraField); free(commentString); #endif return 1; }
static void ctx_push_ruby_object(struct state *state, VALUE obj) { duk_context *ctx = state->ctx; duk_idx_t arr_idx; VALUE str; switch (TYPE(obj)) { case T_FIXNUM: duk_push_int(ctx, NUM2INT(obj)); return; case T_FLOAT: duk_push_number(ctx, NUM2DBL(obj)); return; case T_SYMBOL: #ifdef HAVE_RB_SYM2STR obj = rb_sym2str(obj); #else obj = rb_id2str(SYM2ID(obj)); #endif // Intentional fall-through: case T_STRING: str = encode_cesu8(state, obj); duk_push_lstring(ctx, RSTRING_PTR(str), RSTRING_LEN(str)); return; case T_TRUE: duk_push_true(ctx); return; case T_FALSE: duk_push_false(ctx); return; case T_NIL: duk_push_null(ctx); return; case T_ARRAY: arr_idx = duk_push_array(ctx); for (int idx = 0; idx < RARRAY_LEN(obj); idx++) { ctx_push_ruby_object(state, rb_ary_entry(obj, idx)); duk_put_prop_index(ctx, arr_idx, idx); } return; case T_HASH: duk_push_object(ctx); rb_hash_foreach(obj, ctx_push_hash_element, (VALUE)state); return; default: // Cannot convert break; } clean_raise(ctx, rb_eTypeError, "cannot convert %s", rb_obj_classname(obj)); }
int test_1(duk_context *ctx) { const char *p; size_t sz; duk_set_top(ctx, 0); duk_push_lstring(ctx, "foo\0bar", 7); duk_push_string(ctx, ""); sz = (size_t) 0xdeadbeef; p = duk_require_lstring(ctx, 0, &sz); printf("string: %s (%d)\n", p, (int) sz); sz = (size_t) 0xdeadbeef; p = duk_require_lstring(ctx, 0, NULL); printf("string: %s\n", p); sz = (size_t) 0xdeadbeef; p = duk_require_lstring(ctx, 1, &sz); printf("string: %s (%d)\n", p, (int) sz); sz = (size_t) 0xdeadbeef; p = duk_require_lstring(ctx, 1, NULL); printf("string: %s\n", p); return 0; }
static duk_ret_t test_lstring(duk_context *ctx, void *udata) { duk_idx_t i, n; (void) udata; 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) 0xdeadbeefUL; buf = duk_get_lstring_default(ctx, i, &len, "default!", 7); printf("index %ld: length %lu: ", (long) i, (unsigned long) len); dump((const unsigned char *) buf); buf = duk_get_lstring_default(ctx, i, NULL, "default!", 7); printf("index %ld: ", (long) i); dump((const unsigned char *) buf); } return 0; }
// Sync readfile using libuv APIs as an API function. static duk_ret_t duv_loadfile(duk_context *ctx) { const char* path = duk_require_string(ctx, 0); uv_fs_t req; int fd = 0; uint64_t size; char* chunk; uv_buf_t buf; if (uv_fs_open(&loop, &req, path, O_RDONLY, 0644, NULL) < 0) goto fail; fd = req.result; if (uv_fs_fstat(&loop, &req, fd, NULL) < 0) goto fail; size = req.statbuf.st_size; chunk = duk_alloc(ctx, size); buf = uv_buf_init(chunk, size); if (uv_fs_read(&loop, &req, fd, &buf, 1, 0, NULL) < 0) goto fail; duk_push_lstring(ctx, chunk, size); duk_free(ctx, chunk); uv_fs_close(&loop, &req, fd, NULL); uv_fs_req_cleanup(&req); return 1; fail: if (fd) uv_fs_close(&loop, &req, fd, NULL); uv_fs_req_cleanup(&req); duk_error(ctx, DUK_ERR_ERROR, "%s: %s: %s", uv_err_name(req.result), uv_strerror(req.result), path); }
static duk_ret_t dukzip_unz_listfiles(duk_context *ctx) { unzFile archive = dukzip_unz_from_this(ctx); unzGoToFirstFile(archive); int i = 0, res; duk_idx_t arr_idx = duk_push_array(ctx); do { // unz_file_info fileInfo; // unzGetCurrentFileInfo(archive, &fileInfo, NULL, 0, NULL, 0, NULL, 0); unz_file_info64 fileInfo; unzGetCurrentFileInfo64(archive, &fileInfo, NULL, 0, NULL, 0, NULL, 0); #if defined(__GNUC__) && !defined(DUK_NO_VLA) char fileName[fileInfo.size_filename]; #else char *fileName = malloc(fileInfo.size_filename); #endif // unzGetCurrentFileInfo(archive, &fileInfo, fileName, fileInfo.size_filename, NULL, 0, NULL, 0); unzGetCurrentFileInfo64(archive, &fileInfo, fileName, fileInfo.size_filename, NULL, 0, NULL, 0); duk_push_lstring(ctx, fileName, fileInfo.size_filename); duk_put_prop_index(ctx, arr_idx, i++); #if !defined(__GNUC__) || defined(DUK_NO_VLA) free(fileName); #endif res = unzGoToNextFile(archive); } while (res != UNZ_END_OF_LIST_OF_FILE || res == UNZ_OK); return 1; }
static duk_ret_t test_1(duk_context *ctx) { const char *p; duk_size_t sz; duk_set_top(ctx, 0); duk_push_lstring(ctx, "foo\0bar", 7); duk_push_string(ctx, ""); sz = (duk_size_t) 0xdeadbeef; p = duk_require_lstring(ctx, 0, &sz); dump_string_size(p, sz); sz = (duk_size_t) 0xdeadbeef; p = duk_require_lstring(ctx, 0, NULL); dump_string(p); sz = (duk_size_t) 0xdeadbeef; p = duk_require_lstring(ctx, 1, &sz); dump_string_size(p, sz); sz = (duk_size_t) 0xdeadbeef; p = duk_require_lstring(ctx, 1, NULL); dump_string(p); return 0; }
static duk_ret_t test_1(duk_context *ctx) { duk_size_t i, n; char *buf; duk_set_top(ctx, 0); duk_push_undefined(ctx); duk_push_null(ctx); duk_push_true(ctx); duk_push_false(ctx); duk_push_nan(ctx); duk_push_number(ctx, -INFINITY); duk_push_number(ctx, +INFINITY); duk_push_number(ctx, -0.0); duk_push_number(ctx, +0.0); duk_push_int(ctx, 123); duk_push_string(ctx, "foo"); duk_push_lstring(ctx, "foo\0bar", 7); /* internal NULs are kept */ duk_push_object(ctx); buf = (char *) duk_push_fixed_buffer(ctx, 0); buf = (char *) duk_push_fixed_buffer(ctx, 16); for (i = 0; i < 16; i++) { buf[i] = i; } buf = (char *) duk_push_dynamic_buffer(ctx, 0); buf = (char *) duk_push_dynamic_buffer(ctx, 16); for (i = 0; i < 16; i++) { buf[i] = i; } duk_push_pointer(ctx, (void *) NULL); duk_push_pointer(ctx, (void *) 0xdeadbeef); n = duk_get_top(ctx); printf("top: %ld\n", (long) n); for (i = 0; i < n; i++) { duk_int_t t1, t2; void *ptr; duk_size_t sz; duk_dup(ctx, i); t1 = duk_get_type(ctx, -1); sz = (duk_size_t) 0xdeadbeef; ptr = duk_to_buffer(ctx, -1, &sz); t2 = duk_get_type(ctx, -1); printf("index %ld, type %ld -> %ld, ptr-is-NULL %d, size %lu\n", (long) i, (long) t1, (long) t2, (sz == 0 ? -1 : (ptr == NULL ? 1 : 0)), (unsigned long) sz); dump_buffer(ctx); duk_pop(ctx); /* just check that this doesn't break */ duk_dup(ctx, i); ptr = duk_to_buffer(ctx, -1, NULL); duk_pop(ctx); } return 0; }
static duk_ret_t test_string(duk_context *ctx, void *udata) { duk_idx_t i, n; (void) udata; 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++) { printf("index %ld: ", (long) i); dump((const unsigned char *) duk_get_string_default(ctx, i, "default")); } return 0; }
static duk_ret_t dukzip_unz_getfilename(duk_context *ctx) { // unz_file_info fileInfo; unz_file_info64 fileInfo; unzFile archive = dukzip_unz_from_this(ctx); // unzGetCurrentFileInfo(archive, &fileInfo, NULL, 0, NULL, 0, NULL, 0); unzGetCurrentFileInfo64(archive, &fileInfo, NULL, 0, NULL, 0, NULL, 0); #if defined(__GNUC__) && !defined(DUK_NO_VLA) char fileName[fileInfo.size_filename]; #else char *fileName = malloc(fileInfo.size_filename); #endif // unzGetCurrentFileInfo(archive, &fileInfo, fileName, fileInfo.size_filename, NULL, 0, NULL, 0); unzGetCurrentFileInfo64(archive, &fileInfo, fileName, fileInfo.size_filename, NULL, 0, NULL, 0); duk_push_lstring(ctx, fileName, fileInfo.size_filename); #if !defined(__GNUC__) || defined(DUK_NO_VLA) free(fileName); #endif return 1; }
/* * Read data from a fd. Args: * - 0: fd * - 1: nread * TODO: * - use buffers */ static duk_ret_t io_read(duk_context* ctx) { int fd; ssize_t r; size_t nread; char* buf; fd = duk_require_int(ctx, 0); nread = duk_require_int(ctx, 1); buf = malloc(nread); if (!buf) { SJS_THROW_ERRNO_ERROR2(ENOMEM); return -42; /* control never returns here */ } r = read(fd, buf, nread); if (r < 0) { SJS_THROW_ERRNO_ERROR(); return -42; /* control never returns here */ } else if (r == 0) { /* EOF */ duk_push_string(ctx, ""); } else { duk_push_lstring(ctx, buf, r); } return 1; }
/* in interactive mode, write to stdout */ print_pop_error(ctx, stdout); retval = -1; /* an error 'taints' the execution */ } else { duk_pop(ctx); } } done: if (buffer) { free(buffer); buffer = NULL; } return retval; } #else /* NO_READLINE */ static int handle_interactive(duk_context *ctx) { const char *prompt = "duk> "; char *buffer = NULL; int retval = 0; int rc; duk_eval_string(ctx, GREET_CODE("")); duk_pop(ctx); /* * Note: using readline leads to valgrind-reported leaks inside * readline itself. Execute code from an input file (and not * through stdin) for clean valgrind runs. */ rl_initialize(); for (;;) { if (buffer) { free(buffer); buffer = NULL; } buffer = readline(prompt); if (!buffer) { break; } if (buffer && buffer[0] != (char) 0) { add_history(buffer); } duk_push_lstring(ctx, buffer, strlen(buffer)); duk_push_string(ctx, "input"); if (buffer) { free(buffer); buffer = NULL; } interactive_mode = 1; /* global */ rc = duk_safe_call(ctx, wrapped_compile_execute, 2 /*nargs*/, 1 /*nret*/); if (rc != DUK_EXEC_SUCCESS) { /* in interactive mode, write to stdout */ print_pop_error(ctx, stdout); retval = -1; /* an error 'taints' the execution */ } else { duk_pop(ctx); } } if (buffer) { free(buffer); buffer = NULL; } return retval; }
DUK_LOCAL duk_uint8_t *duk__load_string_raw(duk_context *ctx, duk_uint8_t *p) { duk_uint32_t len; len = DUK_RAW_READ_U32_BE(p); duk_push_lstring(ctx, (const char *) p, len); p += len; return p; }
static duk_ret_t js_CreateStringFromByteArray(duk_context* ctx) { bytearray_t* array = duk_require_sphere_bytearray(ctx, 0); duk_push_lstring(ctx, (char*)array->buffer, array->size); return 1; }
static duk_int_t eval_file(duk_context *ctx, const char *filename) { size_t len; char *buf = read_file(filename, &len); if(!buf) FAIL_LOAD duk_push_lstring(ctx, (const char *)buf, (duk_size_t)len); duk_int_t retval = duk_peval(ctx); duk_pop(ctx); return retval; }
static int handle_fh(duk_context *ctx, FILE *f, const char *filename) { char *buf = NULL; int len; int got; int rc; int retval = -1; if (fseek(f, 0, SEEK_END) < 0) { goto error; } len = (int) ftell(f); if (fseek(f, 0, SEEK_SET) < 0) { goto error; } buf = (char *) malloc(len); if (!buf) { goto error; } got = fread((void *) buf, (size_t) 1, (size_t) len, f); duk_push_lstring(ctx, buf, got); duk_push_string(ctx, filename); free(buf); buf = NULL; interactive_mode = 0; /* global */ rc = duk_safe_call(ctx, wrapped_compile_execute, 2 /*nargs*/, 1 /*nret*/); if (rc != DUK_EXEC_SUCCESS) { print_pop_error(ctx, stderr); goto error; } else { duk_pop(ctx); retval = 0; } /* fall thru */ cleanup: if (buf) { free(buf); } return retval; error: fprintf(stderr, "error in executing file %s\n", filename); fflush(stderr); goto cleanup; }
/* For brevity assumes a maximum file length of 16kB. */ static void push_file_as_string(duk_context *ctx, const char *filename) { FILE *f; size_t len; char buf[16384]; f = fopen(filename, "rb"); if (f) { len = fread((void *) buf, 1, sizeof(buf), f); fclose(f); duk_push_lstring(ctx, (const char *) buf, (duk_size_t) len); } else { duk_push_undefined(ctx); } }
static duk_ret_t test_1b(duk_context *ctx, void *udata) { const char *p; (void) udata; duk_set_top(ctx, 0); printf("push string with maximum size_t - 8 (should fail)\n"); p = duk_push_lstring(ctx, dummy, DUK_SIZE_MAX - 8); printf("ptr is non-NULL: %d\n", (p != NULL ? 1 : 0)); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
int main(int argc, char *argv[]) { int i, rc; prg.argc = argc; prg.argv = argv; prg.name = argv[1]; duk_context *ctx = duk_create_heap_default(); duk_push_global_object(ctx); duk_push_object(ctx); /* -> [ ... global obj ] */ sys1(ctx); for(i=1;i<argc;i++) { duk_push_string(ctx, argv[i]); duk_put_prop_index(ctx, -2, i-1); } duk_push_number(ctx, argc-1); duk_put_prop_string(ctx, -2, "argc"); duk_put_prop_string(ctx, -2, "Sys1"); /* -> [ ... global ] */ duk_push_object(ctx); /* -> [ ... global obj ] */ prg1(ctx); duk_put_prop_string(ctx, -2, "Prg1"); /* -> [ ... global ] */ prg_push_modsearch(ctx); duk_pop(ctx); prg_parse_appfile(&prg); // push file duk_push_lstring(ctx, prg.main->buf, prg.main->size); duk_push_string(ctx, argv[1]); // execute file (compile + call) prg_register(&prg); rc = duk_safe_call(ctx, prg_wrapped_compile_execute, 2 /*nargs*/, 1 /*nret*/); if (rc != DUK_EXEC_SUCCESS) { print_error(ctx, stderr); exit(2); } duk_pop(ctx); /* pop eval result */ duk_destroy_heap(ctx); return prg.status; }
int SnssFileAPI::readUTF16(duk_context* ctx) { int nRes = 1; int nLength = duk_get_int(ctx, -1); nLength *= sizeof(wchar_t); char* chBuf = new char[nLength]; m_SnssFile.read(chBuf, nLength); if( m_SnssFile.eof() ) nRes = 0; else duk_push_lstring(ctx, chBuf, nLength); delete [] chBuf; return nRes; }
static void ctx_get_one_prop(struct state *state, VALUE name, int strict) { duk_context *ctx = state->ctx; // Don't allow prop access on undefined/null if (duk_check_type_mask(ctx, -1, DUK_TYPE_MASK_UNDEFINED | DUK_TYPE_MASK_NULL)) { clean_raise(ctx, eTypeError, "invalid base value"); } duk_push_lstring(ctx, RSTRING_PTR(name), RSTRING_LEN(name)); duk_bool_t exists = duk_get_prop(ctx, -2); if (!exists && strict) { const char *str = StringValueCStr(name); clean_raise(ctx, eReferenceError, "identifier '%s' undefined", str); } }
// Duktape.modSearch function, needed for loading modules with require() duk_ret_t module_search(duk_context *ctx) { const char *id = duk_require_string(ctx, 0); // C modules: add functions to exports variable (3rd argument) and return undefined for (int i = 0; i < c_module_count; i++) { if (!strcmp(c_module_list[i].name, id)) { duk_push_c_function(ctx, c_module_list[i].init_func, 0); duk_call(ctx, 0); duk_enum(ctx, -1, 0); while(duk_next(ctx, -1, 1)) { duk_put_prop(ctx, 2); } duk_pop_2(ctx); return 0; } } // JS modules: return source code as a string // Read from file "modname.js.tns" int module_filename_len = strlen(id) + strlen(".js.tns") + 1; char *module_filename = malloc(module_filename_len); if (!module_filename) goto error; snprintf(module_filename, module_filename_len, "%s.js.tns", id); FILE *module_file = fopen(module_filename, "r"); free(module_filename); if (!module_file) goto error; if (fseek(module_file, 0, SEEK_END) != 0) goto error; long module_file_size = ftell(module_file); if (module_file_size == -1) goto error; rewind(module_file); char *src = malloc(module_file_size); if (!src) goto error; fread(src, 1, module_file_size, module_file); if (ferror(module_file)) goto error; fclose(module_file); duk_push_lstring(ctx, src, module_file_size); free(src); return 1; error: duk_push_error_object(ctx, DUK_ERR_ERROR, "module %s not found: %s", id, strerror(errno)); duk_throw(ctx); }
static int lang_duktape_file(RLang *lang, const char *file) { int ret = -1; char *code = r_file_slurp (file, NULL); if (code) { register_helpers (lang); duk_push_lstring (ctx, code, strlen (code)); duk_push_string (ctx,file); free (code); ret = duk_safe_call (ctx, wrapped_compile_execute, 2, 1); if (ret != DUK_EXEC_SUCCESS) { print_error(ctx, stderr); eprintf ("duktape error"); } else { duk_pop (ctx); } } return ret; }
static int lang_duktape_safe_eval(duk_context *ctx, const char *code) { #if UNSAFE duk_eval_string (ctx, code); #else int rc; duk_push_lstring (ctx, code, strlen (code)); duk_push_string (ctx, "input"); rc = duk_safe_call (ctx, wrapped_compile_execute, 2, 1); //, DUK_INVALID_INDEX); if (rc != DUK_EXEC_SUCCESS) { print_error(ctx, stderr); rc = R_FALSE; } else { duk_pop (ctx); rc = R_TRUE; } return rc; #endif }
static duk_ret_t test_vary_size(duk_context *ctx, void *udata) { duk_size_t i; unsigned char buf[1024]; (void) udata; for (i = 0; i < 1024; i++) { buf[i] = (duk_uint8_t) (i & 0x7f); } for (i = 0; i < 1024; i++) { duk_push_lstring(ctx, (const char *) buf, i); duk_map_string(ctx, -1, map_char_2, NULL); duk_pop(ctx); } printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
void init_env(duk_context *ctx) { // TODO: It might be cleaner to make this a property on some Process object // that can also have some other metadata (e.g. command line args) duk_push_global_object(ctx); duk_push_object(ctx); for (char **env=environ ; *env!=NULL ; env++) { char *kv = *env; char *val = strchr(kv, '='); duk_push_lstring(ctx, kv, val-kv); // Skip the = duk_push_string(ctx, val+1); duk_put_prop(ctx, -3); } duk_put_prop_string(ctx, -2, "environ"); duk_pop(ctx); }
static int HttpRequest_Read(duk_context* ctx) { duk_push_this(ctx); HttpRequest* request = js_to_class_instance<HttpRequest>(ctx, -1, 0); if (!request->GetAvailableSize()) { duk_push_string(ctx, ""); return 1; } PODVector<unsigned char> buffer(request->GetAvailableSize()); request->Read(&buffer[0], buffer.Size()); duk_push_lstring(ctx, (const char*) &buffer[0], buffer.Size()); return 1; }
static duk_ret_t js_RawFile_readString(duk_context* ctx) { size_t num_bytes = duk_require_uint(ctx, 0); FILE* file; void* read_buffer; duk_push_this(ctx); file = duk_require_sphere_obj(ctx, -1, "RawFile"); duk_pop(ctx); if (num_bytes <= 0 || num_bytes > INT_MAX) duk_error_ni(ctx, -1, DUK_ERR_RANGE_ERROR, "RawFile:read(): Read size out of range (%i)", num_bytes / 1048576); if (file == NULL) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:read(): File has been closed"); if (!(read_buffer = malloc(num_bytes))) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:read(): Failed to allocate buffer for file read"); num_bytes = fread(read_buffer, 1, num_bytes, file); duk_push_lstring(ctx, read_buffer, num_bytes); return 1; }
void test(duk_context *ctx) { const char *res; duk_push_undefined(ctx); PRINTTOP(); duk_push_null(ctx); PRINTTOP(); duk_push_true(ctx); PRINTTOP(); duk_push_false(ctx); PRINTTOP(); duk_push_boolean(ctx, -1); PRINTTOP(); duk_push_boolean(ctx, 0); PRINTTOP(); duk_push_boolean(ctx, 1); PRINTTOP(); duk_push_number(ctx, 123.4); PRINTTOP(); duk_push_int(ctx, 234); PRINTTOP(); duk_push_nan(ctx); PRINTTOP(); res = duk_push_string(ctx, "foo"); PRINTRESTOP(); res = duk_push_string(ctx, "foo\0bar\0"); PRINTRESTOP(); res = duk_push_string(ctx, ""); PRINTRESTOP(); /* pushes empty */ res = duk_push_string(ctx, NULL); PRINTRESTOP(); /* pushes a NULL */ res = duk_push_lstring(ctx, "foobar", 4); PRINTRESTOP(); res = duk_push_lstring(ctx, "foob\0\0", 6); PRINTRESTOP(); res = duk_push_lstring(ctx, "\0", 1); PRINTRESTOP(); /* pushes 1-byte string (0x00) */ res = duk_push_lstring(ctx, "\0", 0); PRINTRESTOP(); /* pushes empty */ res = duk_push_lstring(ctx, NULL, 0); PRINTRESTOP(); /* pushes empty */ res = duk_push_lstring(ctx, NULL, 10); PRINTRESTOP(); /* pushes empty */ res = duk_push_sprintf(ctx, "foo"); PRINTRESTOP(); res = duk_push_sprintf(ctx, "foo %d %s 0x%08lx", 123, "bar", (long) 0x1234cafe); PRINTRESTOP(); res = duk_push_sprintf(ctx, ""); PRINTRESTOP(); res = duk_push_sprintf(ctx, NULL); PRINTRESTOP(); res = test_vsprintf_3x_int(ctx, 2, 3, 5); PRINTRESTOP(); res = test_vsprintf_empty(ctx, 2, 3, 5); PRINTRESTOP(); res = test_vsprintf_null(ctx, 2, 3, 5); PRINTRESTOP(); duk_push_pointer(ctx, (void *) 0); PRINTTOP(); duk_push_pointer(ctx, (void *) 0xdeadbeef); PRINTTOP(); }