Beispiel #1
0
void dump_error(duk_context *ctx) {
	duk_dup(ctx, -1);
	printf("ToString(error): %s\n", duk_to_string(ctx, -1));
	duk_pop(ctx);

	duk_get_prop_string(ctx, -1, "name");
	printf("name: %s\n", duk_to_string(ctx, -1));
	duk_pop(ctx);

	duk_get_prop_string(ctx, -1, "message");
	printf("message: %s\n", duk_to_string(ctx, -1));
	duk_pop(ctx);

	duk_get_prop_string(ctx, -1, "code");
	printf("code: %d\n", duk_get_int(ctx, -1));
	duk_pop(ctx);

	duk_get_prop_string(ctx, -1, "fileName");
	printf("fileName: %s\n", duk_to_string(ctx, -1));
	duk_pop(ctx);

	duk_get_prop_string(ctx, -1, "lineNumber");
	printf("lineNumber: %d\n", duk_get_int(ctx, -1));
	duk_pop(ctx);

	duk_get_prop_string(ctx, -1, "isNative");
	printf("isNative: %s\n", duk_to_string(ctx, -1));
	duk_pop(ctx);
}
Beispiel #2
0
duk_ret_t nsp_texture_display(duk_context *ctx) {
    duk_push_this(ctx);
    duk_get_prop_string(ctx, -1, "width");
    int width = duk_get_int(ctx, -1);
    duk_pop(ctx);
    duk_get_prop_string(ctx, -1, "height");
    int height = duk_get_int(ctx, -1);
    duk_pop(ctx);
    duk_get_prop_string(ctx, -1, "transparentColor");
    if (width != 320 || height != 240 || !duk_is_null(ctx, -1)) {
	duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "must have dimensions 230x240 without transparency");
	duk_throw(ctx);
    }
    duk_pop(ctx);
    duk_get_prop_string(ctx, -1, "bitmap");
    size_t size;
    uint16_t *bitmap;
    bitmap = duk_get_buffer(ctx, -1, &size);
    if (bitmap == NULL || size != 320 * 240 * 2) {
	duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap buffer does not match with dimensions");
	duk_throw(ctx);
    }
    memcpy(SCREEN_BASE_ADDRESS, bitmap, 320 * 240 * 2);
    return 0;
}
Beispiel #3
0
int js_algo_neighbors_of(duk_context *ctx) {
	const int xp = duk_get_int(ctx, 0);
	const int yp = duk_get_int(ctx, 1);
	const int id = duk_get_int(ctx, 2);

	const int n = algo_neighbors_of(js_level, js_level_width, js_level_height, xp, yp, id);
	duk_push_int(ctx, n);

	return 1;
}
Beispiel #4
0
/* sets block x|y to id */
int js_level_set(duk_context *ctx) {
	const int x = duk_get_int(ctx, 0);
	const int y = duk_get_int(ctx, 1);
	const int id = duk_get_int(ctx, 2);
	
	/* bound checking */
	if (x < 0 || x >= js_level_width || y < 0 || y >= js_level_height)
		return 0;
	
	js_level[x + y * js_level_width] = id;
	return 0;
}
Beispiel #5
0
/* registers a block r,g,b and returns it's id */
int js_register_block(duk_context *ctx) {
	/* get r, g, b values */
	const int r = duk_get_int(ctx, 0);
	const int g = duk_get_int(ctx, 1);
	const int b = duk_get_int(ctx, 2);
	
	/* registers block, may allocate more memory */
	register_block(r, g, b);
	
	/* return block's id */
	duk_push_int(ctx, blocks_len - 1);

	return 1;
}
static duk_ret_t test_error_augment(duk_context *ctx, void *udata) {
	duk_int_t ret;

	(void) udata;

	prep(ctx);

	/* This case is a bit tricky.  There used to be a problem where the
	 * error augmentation process itself failed when checking whether or
	 * not the throw value inherited from Error.  This has now been fixed
	 * and the error value no longer gets augmented and is thrown correctly.
	 *
	 * The TEST_SAFE_CALL() wrapper uses duk_safe_to_string() to coerce
	 * the throw result.  Since the object doesn't have a toString()
	 * function, this coercion will fail and generate a prototype loop
	 * error!
	 *
	 * So, use a separate duk_safe_call() wrapping here to ensure we treat
	 * the final result value carefully.  We print out 'foo' to be sure
	 * the correct value was thrown.
	 */

	duk_dup(ctx, 0);
	ret = duk_safe_call(ctx, augment_raw, NULL, 0 /*nargs*/, 1 /*nrets*/);
	printf("ret=%d\n", (int) ret);
	duk_get_prop_string(ctx, -1, "foo");
	printf("throw value .foo=%d\n", duk_get_int(ctx, -1));
	duk_pop_2(ctx);

	return 0;
}
Beispiel #7
0
static duk_ret_t nsp_texture_constructor(duk_context *ctx) {
    int width = duk_require_int(ctx, 0);
    int height = duk_require_int(ctx, 1);
    if (width < 1 || height < 1) {
	duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "Width and height must be positive");
	duk_throw(ctx);
    }
    bool has_transparency;
    uint16_t transparent_color;
    if ((has_transparency = duk_is_number(ctx, 2))) {
	transparent_color = (uint16_t)duk_get_int(ctx, 2);
    }
    duk_push_this(ctx);
    duk_push_fixed_buffer(ctx, width * height * 2);
    duk_put_prop_string(ctx, -2, "bitmap");
    duk_push_int(ctx, width);
    duk_put_prop_string(ctx, -2, "width");
    duk_push_int(ctx, height);
    duk_put_prop_string(ctx, -2, "height");
    if (has_transparency) {
	duk_push_int(ctx, transparent_color);
    } else {
	duk_push_null(ctx);
    }
    duk_put_prop_string(ctx, -2, "transparentColor");
    return 0;
}
Beispiel #8
0
static duk_ret_t dukzip_zip_newfile(duk_context *ctx) {
	zip_fileinfo zi = {0};
	int res = ZIP_OK;
	zipFile archive = dukzip_zip_from_this(ctx);
	
	const char *filename = "";
	duk_int_t level = Z_DEFAULT_COMPRESSION;
	duk_int_t method = Z_DEFLATED;
	const char *comment = "";


	if (duk_is_object(ctx, 0)) {
		dukzip_zip_checkoptions(ctx, 0, &filename, &level, &method, &comment);
	} else {
		filename = duk_require_string(ctx, 0);

		if (duk_is_number(ctx, 1)) {
			level = duk_get_int(ctx, 1);
		}
	}

	res = zipOpenNewFileInZip64(archive, filename, &zi, NULL, 0, NULL, 0, comment, method, level, 1);

	if (res == ZIP_OK) {
		duk_push_true(ctx);
	} else {
		duk_push_false(ctx);
	}
	return 1;
}
Beispiel #9
0
void dump_error(duk_context *ctx) {
	duk_dup(ctx, -1);
	printf("ToString(error): %s\n", duk_to_string(ctx, -1));
	duk_pop(ctx);

	duk_get_prop_string(ctx, -1, "name");
	printf("name: %s\n", duk_to_string(ctx, -1));
	duk_pop(ctx);

	duk_get_prop_string(ctx, -1, "message");
	printf("message: %s\n", duk_to_string(ctx, -1));
	duk_pop(ctx);

	/* 'code' is no longer set, test that it reads back as 'undefined' */
	duk_get_prop_string(ctx, -1, "code");
	printf("code: %s\n", duk_to_string(ctx, -1));
	duk_pop(ctx);

	duk_get_prop_string(ctx, -1, "fileName");
	printf("fileName is a string: %d\n", (int) duk_is_string(ctx, -1));
	duk_pop(ctx);

	duk_get_prop_string(ctx, -1, "lineNumber");
	printf("lineNumber: %ld\n", (long) duk_get_int(ctx, -1));
	duk_pop(ctx);

	/* 'isNative' has also been removed, check that it reads back as 'undefined' */
	duk_get_prop_string(ctx, -1, "isNative");
	printf("isNative: %s\n", duk_to_string(ctx, -1));
	duk_pop(ctx);
}
Beispiel #10
0
static int
es_faprovider_readRespond(duk_context *ctx)
{
  es_fa_handle_t *fah = es_get_native_obj(ctx, 0, &es_native_fah);
  int size = duk_get_int(ctx, 1);
  if(size < 0) {
    hts_mutex_lock(&es_fa_mutex);
    if(fah->fah_status == ES_FA_WORKING) {
      fah->fah_status = ES_FA_ERROR;
      hts_cond_broadcast(&es_fa_cond);
    }
    hts_mutex_unlock(&es_fa_mutex);
  } else {
    fah_ok_val(fah, duk_get_int(ctx, 1));
  }
  return 0;
}
Beispiel #11
0
/* get block id */
int js_level_get(duk_context *ctx) {
	/* use x|y coordinates */
	const int x = duk_get_int(ctx, 0);
	const int y = duk_get_int(ctx, 1);
	
	/* do bounds check, return block id 0 -> void-block */
	if (x < 0 || x >= js_level_width || y < 0 || y >= js_level_height) {
		duk_push_int(ctx, 0);
		return 1;
	}
	
	/* get block id */
	const int id = js_level[x + y * js_level_width];
	duk_push_int(ctx, id);

	return 1;
}
Beispiel #12
0
duk_bool_t duv_is_handle_of(duk_context *ctx, int index, duv_type_mask_t mask) {
  if (!duk_is_object(ctx, index)) return 0;
  duk_get_prop_string(ctx, index, "\xff""uv-type");
  int type = duk_get_int(ctx, -1);
  duk_bool_t is = (1 << type) & mask;
  duk_pop(ctx);
  return is;
}
Beispiel #13
0
/*
 * [0] - hostname
 * [1] - socket fd
 */
static duk_ret_t js_ssl_create_dukf_ssl_context(duk_context *ctx) {
	const char *hostname = duk_get_string(ctx, -2);
	int fd = duk_get_int(ctx, -1);
	LOGD(">> js_ssl_create_dukf_ssl_context: hostname=\"%s\", fd=%d", hostname, fd);
	dukf_ssl_context_t *dukf_ssl_context = create_dukf_ssl_context(hostname, fd);
	duk_push_pointer(ctx, dukf_ssl_context);
	LOGD("<< js_ssl_create_dukf_ssl_context");
	return 1;
} // js_ssl_createSSLSocket
static duk_ret_t req_valid_idx(duk_context *ctx) {
	duk_idx_t idx = duk_get_int(ctx, -1);

	duk_pop(ctx);
	printf("req_valid_idx: top %ld after popping arg\n", (long) duk_get_top(ctx));

	duk_require_valid_index(ctx, idx);
	duk_push_true(ctx);
	return 1;
}
Beispiel #15
0
DUK_LOCAL duk_ret_t duk__pcall_prop_raw(duk_context *ctx) {
	duk_idx_t obj_index;
	duk_idx_t nargs;

	/* Get the original arguments.  Note that obj_index may be a relative
	 * index so the stack must have the same top when we use it.
	 */

	DUK_ASSERT_CTX_VALID(ctx);

	obj_index = (duk_idx_t) duk_get_int(ctx, -2);
	nargs = (duk_idx_t) duk_get_int(ctx, -1);
	duk_pop_2(ctx);

	obj_index = duk_require_normalize_index(ctx, obj_index);  /* make absolute */
	duk__call_prop_prep_stack(ctx, obj_index, nargs);
	duk_call_method(ctx, nargs);
	return 1;
}
Beispiel #16
0
/* casts a ray in a direction and returns a point where it collides */
int js_algo_raycast(duk_context *ctx) {
	/* get x, y, dir */
	const int x = duk_to_int(ctx, 0);
	const int y = duk_to_int(ctx, 1);
	const int dir = duk_to_int(ctx, 2);
	/* how many ignored blocks */
	duk_get_prop_string(ctx, 3, "length");
	const int ids_len = duk_get_int(ctx, -1);
	/* allocate memory for ignored blocks */
	int *ids = malloc(sizeof(int) * ids_len);
	/* get all ids to be ignored */
	for (int i = 0; i < ids_len; ++i) {
		duk_get_prop_index(ctx, 3, i);
		ids[i] = duk_get_int(ctx, -1);
		duk_pop(ctx);
	}
	
	/* calculate distance in dir */
	const int distance = algo_distance(js_level, js_level_width,
								js_level_height,  x, y, dir, ids, ids_len);
	/* no longer need ignored_ids */
	free(ids);
	
	/* return position in array */
	const int arr_idx = duk_push_array(ctx);
	if (dir == 1)
		duk_push_int(ctx, x + distance);
	else if (dir == -1)
		duk_push_int(ctx, x - distance);
	else
		duk_push_int(ctx, x);	
	duk_put_prop_index(ctx, arr_idx, 0);

	if (dir == -2)
		duk_push_int(ctx, y - distance);
	else if (dir == 2)
		duk_push_int(ctx, y + distance);
	else
		duk_push_int(ctx, y);	
	duk_put_prop_index(ctx, arr_idx, 1);
	
	return 1;
}
Beispiel #17
0
static void dump_stack(duk_context *ctx) {
	duk_idx_t i, n;

	printf("[");
	n = duk_get_top(ctx);
	for (i = 0; i < n; i++) {
		printf(" %ld", (long) duk_get_int(ctx, i));
	}
	printf(" ]\n");
}
Beispiel #18
0
int req_norm_idx(duk_context *ctx) {
	int idx = duk_get_int(ctx, -1);
	int norm_idx;

	duk_pop(ctx);
	printf("req_norm_idx: top %d after popping arg\n", duk_get_top(ctx));

	norm_idx = duk_require_normalize_index(ctx, idx);
	duk_push_int(ctx, norm_idx);
	return 1;
}
Beispiel #19
0
int SnssFileAPI::skipBytes(duk_context* ctx)
{
    int nRes = 1;
    int nLength = duk_get_int(ctx, -1);
    char* chBuf = new char[nLength];
    m_SnssFile.read(chBuf, nLength);
    if( m_SnssFile.eof() )
        nRes = 0;
    delete [] chBuf;
    return 1;
}
Beispiel #20
0
/*
 * Set the mbedtls sebug threshold.
 * 0 – no debug
 * 1 – error
 * 2 – state change
 * 3 – informational
 * 4 – verbose
 * [0] - Debug level
 */
static duk_ret_t js_ssl_debug_set_threshold(duk_context *ctx) {
	LOGD(">> js_ssl_debug_set_threshold");
	int threshold = duk_get_int(ctx, -1);
	if (threshold < 0 || threshold > 4) {
		LOGE("bad threshold");
	} else {
		mbedtls_debug_set_threshold(threshold);
	}
	LOGD("<< js_ssl_debug_set_threshold");
	return 0;
} // js_ssl_debug_set_threshold
Beispiel #21
0
static int NativePinSetTrigger(duk_context* ctx)
{
    uint32_t debounce = 0;
    AJS_IO_PinTriggerCondition condition = (AJS_IO_PinTriggerCondition)duk_require_int(ctx, 0);
    if (condition & AJS_IO_PIN_TRIGGER_ON_BOTH) {
        if (duk_is_number(ctx, 2)) {
            debounce = duk_get_int(ctx, 2);
            debounce = min(255, debounce);
        }
    }
    return SetTriggerCallback(ctx, AJS_IO_FUNCTION_DIGITAL_IN, debounce);
}
Beispiel #22
0
void test(duk_context *ctx) {
	duk_eval_string(ctx, "1+2");
	printf("1+2=%d\n", (int) duk_get_int(ctx, -1));
	duk_pop(ctx);

	duk_push_c_function(ctx, native_print, 1);
	duk_put_global_string(ctx, "print");
	duk_push_c_function(ctx, native_adder, DUK_VARARGS);
	duk_put_global_string(ctx, "adder");

	duk_eval_string_noresult(ctx, "print('2+3=' + adder(2, 3));");
}
int duk_builtin_duk_object_gc(duk_context *ctx) {
#ifdef DUK_USE_MARK_AND_SWEEP
	duk_hthread *thr = (duk_hthread *) ctx;
	int flags;
	int rc;

	flags = duk_get_int(ctx, 0);
	rc = duk_heap_mark_and_sweep(thr->heap, flags);
	duk_push_int(ctx, rc);
	return 1;
#else
	return 0;
#endif
}
Beispiel #24
0
/*
 * Serializes various data types into a buffer. Leaves the buffer on the top of the stack.
 */
static uint8_t* SerializeToBuffer(duk_context* ctx, duk_idx_t idx, duk_size_t* sz)
{
    uint8_t* ptr;

    switch (duk_get_type(ctx, idx)) {
    case DUK_TYPE_BUFFER:
        duk_dup(ctx, idx);
        ptr = duk_get_buffer(ctx, -1, sz);
        break;

    case DUK_TYPE_BOOLEAN:
        ptr = duk_push_fixed_buffer(ctx, 1);
        ptr[0] = duk_get_boolean(ctx, idx);
        *sz = 1;
        break;

    case DUK_TYPE_NUMBER:
        ptr = duk_push_fixed_buffer(ctx, 1);
        ptr[0] = duk_get_int(ctx, idx);
        *sz = 1;
        break;

    case DUK_TYPE_STRING:
        duk_dup(ctx, idx);
        ptr = duk_to_fixed_buffer(ctx, -1, sz);
        break;

    case DUK_TYPE_OBJECT:
        if (duk_is_array(ctx, idx)) {
            duk_idx_t i;
            duk_idx_t len = duk_get_length(ctx, idx);
            ptr = duk_push_fixed_buffer(ctx, len);
            for (i = 0; i < len; ++i) {
                duk_get_prop_index(ctx, idx, i);
                ptr[i] = duk_require_uint(ctx, -1);
                duk_pop(ctx);
            }
            *sz = len;
        } else {
            duk_error(ctx, DUK_ERR_TYPE_ERROR, "Can only serialize arrays of numbers");
        }
        break;

    default:
        duk_error(ctx, DUK_ERR_TYPE_ERROR, "Cannot serialize");
        break;
    }
    return ptr;
}
static int NativeSignal(duk_context* ctx)
{
    const char* dest;
    uint32_t session;

    duk_push_this(ctx);
    duk_get_prop_string(ctx, -1, "dest");
    dest = duk_get_string(ctx, -1);
    duk_pop(ctx);
    duk_get_prop_string(ctx, -1, "session");
    session = duk_get_int(ctx, -1);
    duk_pop_2(ctx);
    InitSignal(ctx, dest, session);
    return 1;
}
Beispiel #26
0
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;
}
Beispiel #27
0
const char * duk_js_error_to_string(duk_context *ctx, int index){
	DEBUG_LOG("ScriptEngine","duk_js_error_to_string %d  %d", index, duk_is_error(ctx, index));
	if(duk_get_prop_string(ctx, index, "lineNumber")){
         int lineNumber = duk_get_int(ctx, -1);
         duk_pop(ctx);
         duk_push_sprintf(ctx, " %s  at javascript source %d line", duk_to_string(ctx, index), lineNumber);
         if(index < 0){
            duk_replace(ctx, index - 1);
         }else{
        	    duk_replace(ctx, index);
         }
         return duk_to_string(ctx,index);
	}
	duk_pop(ctx);
	return duk_to_string(ctx, index);
}
Beispiel #28
0
void test(duk_context *ctx) {
	printf("top: %d\n", duk_get_top(ctx));
	duk_push_heap_stash(ctx);
	printf("top: %d\n", duk_get_top(ctx));

	duk_push_int(ctx, 123);
	duk_put_prop_string(ctx, -2, "myvalue");
	duk_pop(ctx);
	printf("top: %d\n", duk_get_top(ctx));

	duk_push_heap_stash(ctx);
	duk_get_prop_string(ctx, -1, "myvalue");
	printf("value: %d\n", duk_get_int(ctx, -1));
	duk_pop(ctx);
	duk_pop(ctx);
	printf("top: %d\n", duk_get_top(ctx));
}
Beispiel #29
0
static void dukzip_zip_checkoptions(duk_context *ctx, duk_idx_t idx, const char **filename, duk_int_t *level, duk_int_t *method, const char **comment) 
{
	duk_get_prop_string(ctx, idx, "filename");
	if (duk_is_string(ctx, -1)) {
		*filename = duk_get_string(ctx, -1);
		duk_pop(ctx);
	} else {
		duk_pop(ctx);
	}

	duk_get_prop_string(ctx, idx, "level");
	if (duk_is_number(ctx, -1)) {
		*level = duk_get_int(ctx, -1);
		duk_pop(ctx);
	} else {
		duk_pop(ctx);
	}

	duk_get_prop_string(ctx, idx, "method");
	if (duk_is_string(ctx, -1)) {
		duk_push_string(ctx, "deflate");
		if (duk_equals(ctx, -1, -2)) {
			*method = Z_DEFLATED;
		} else {
			duk_pop(ctx);
			duk_push_string(ctx, "store");
			if (duk_equals(ctx, -1, -2)) {
				*method = 0;
			}
		}
		duk_pop_2(ctx);
	} else {
		duk_pop(ctx);
	}

	duk_get_prop_string(ctx, idx, "comment");
	if (duk_is_string(ctx, -1)) {
		*comment = duk_get_string(ctx, -1);
		duk_pop(ctx);
	} else {
		duk_pop(ctx);
	}
}
Beispiel #30
0
/*
 * Configures a pin as a digital input pin
 */
static int NativeIoDigitalIn(duk_context* ctx)
{
    AJ_Status status;
    int idx;
    void* pinCtx;
    int config = -1;
    uint32_t pin = GetPinId(ctx, 0, AJS_IO_FUNCTION_DIGITAL_IN);

    if (duk_is_undefined(ctx, 1)) {
        config = AJS_IO_PIN_PULL_UP;
    } else if (duk_is_number(ctx, 1)) {
        config = (AJS_IO_PinConfig)duk_get_int(ctx, 1);
    }
    if ((config != AJS_IO_PIN_OPEN_DRAIN) && (config != AJS_IO_PIN_PULL_UP) && (config != AJS_IO_PIN_PULL_DOWN)) {
        duk_error(ctx, DUK_ERR_RANGE_ERROR, "Configuration must be pullUp, pullDown, or openDrain");
    }
    /*
     * Target specific I/O pin initialization
     */
    status = AJS_TargetIO_PinOpen(pin, (AJS_IO_PinConfig)config, &pinCtx);
    if (status != AJ_OK) {
        duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Failed to open digital input pin: %s", AJ_StatusText(status));
    }
    idx = NewIOObject(ctx, pinCtx, AJS_IO_FUNCTION_DIGITAL_IN, NativePinFinalizer);
    /*
     * Function to get the pin level
     */
    AJS_SetPropertyAccessors(ctx, idx, "level", NULL, NativeLevelGetter);
    /*
     * Function to set and clear a trigger
     */
    duk_push_c_lightfunc(ctx, NativePinSetTrigger, 3, 0, 0);
    duk_put_prop_string(ctx, idx, "setTrigger");
    duk_push_c_lightfunc(ctx, NativePinClearTrigger, 1, 0, 0);
    duk_put_prop_string(ctx, idx, "clearTrigger");
    /*
     * Return the digital input pin object
     */
    return 1;
}