Example #1
0
static duk_ret_t
js_ByteArray_getProp(duk_context* ctx)
{
	bytearray_t* array;
	int          index;
	int          size;

	duk_get_prop_string(ctx, 0, "\xFF" "ptr"); array = duk_get_pointer(ctx, -1); duk_pop(ctx);
	if (duk_is_number(ctx, 1)) {
		index = duk_to_int(ctx, 1);
		size = get_bytearray_size(array);
		if (index < 0 || index >= size)
			duk_error_ni(ctx, -1, DUK_ERR_RANGE_ERROR, "ByteArray[]: Index is out of bounds (%i - size: %i)", index, size);
		duk_push_uint(ctx, get_byte(array, index));
		return 1;
	}
	else {
		duk_dup(ctx, 1);
		duk_get_prop(ctx, 0);
		return 1;
	}
}
Example #2
0
static duk_ret_t
js_RawFile_write(duk_context* ctx)
{
	bytearray_t* array;
	const void*  data;
	FILE*        file;
	size_t       write_size;

	duk_push_this(ctx);
	file = duk_require_sphere_obj(ctx, -1, "RawFile");
	duk_pop(ctx);
	if (file == NULL)
		duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:write(): File has been closed");
	if (duk_is_string(ctx, 0))
		data = duk_get_lstring(ctx, 0, &write_size);
	else {
		array = duk_require_sphere_obj(ctx, 0, "ByteArray");
		data = get_bytearray_buffer(array);
		write_size = get_bytearray_size(array);
	}
	if (fwrite(data, 1, write_size, file) != write_size)
		duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:write(): Write error. The file may be read-only.");
	return 0;
}