当前位置: 首页>>代码示例>>C++>>正文


C++ duk_error函数代码示例

本文整理汇总了C++中duk_error函数的典型用法代码示例。如果您正苦于以下问题:C++ duk_error函数的具体用法?C++ duk_error怎么用?C++ duk_error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了duk_error函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: get_filename

static const char *
get_filename(duk_context *ctx, int index, const es_context_t *ec,
             int for_write)
{
  const char *filename = duk_to_string(ctx, index);

  if(gconf.bypass_ecmascript_acl)
    return filename;

  if(for_write && ec->ec_bypass_file_acl_write)
    return filename;

  if(!for_write && ec->ec_bypass_file_acl_read)
    return filename;

  if(strstr(filename, "../") || strstr(filename, "/.."))
    duk_error(ctx, DUK_ERR_ERROR,
              "Bad filename %s -- Contains parent references", filename);

  if((ec->ec_storage != NULL && mystrbegins(filename, ec->ec_storage)) ||
     (ec->ec_path    != NULL && mystrbegins(filename, ec->ec_path)))
    return filename;

  duk_error(ctx, DUK_ERR_ERROR, "Bad filename %s -- Access not allowed",
            filename);
}
开发者ID:Ezio-PS,项目名称:movian,代码行数:26,代码来源:es_fs.c

示例2: duv_loadlib

// Load a duktape C function from a shared library by path and name.
static duk_ret_t duv_loadlib(duk_context *ctx) {
  const char *name, *path;
  uv_lib_t lib;
  duk_c_function fn;

  // Check the args
  const duv_schema_entry schema[] = {
    { "path", duk_is_string },
    { "name", duk_is_string },
    { NULL, NULL }
  };

  dschema_check(ctx, schema);

  path = duk_get_string(ctx, 0);
  name = duk_get_string(ctx, 1);

  if (uv_dlopen(path, &lib)) {
    duk_error(ctx, DUK_ERR_ERROR, "Cannot load shared library %s", path);
    return 0;
  }
  if (uv_dlsym(&lib, name, (void**)&fn)) {
    duk_error(ctx, DUK_ERR_ERROR, "Unable to find %s in %s", name, path);
    return 0;
  }
  duk_push_c_function(ctx, fn, 0);
  return 1;
}
开发者ID:madanagopalt,项目名称:pxCore,代码行数:29,代码来源:rtScriptDuk.cpp

示例3: es_file_read

/**
 * fd, buffer, offset, length, position
 */
static int
es_file_read(duk_context *ctx)
{
  es_fd_t *efd = es_fd_get(ctx, 0);
  duk_size_t bufsize;
  char *buf = duk_require_buffer_data(ctx, 1, &bufsize);

  const int offset = duk_to_int(ctx, 2);
  const int len = duk_to_int(ctx, 3);

  if(offset + len > bufsize)
    duk_error(ctx, DUK_ERR_ERROR, "Buffer too small %zd < %d + %d",
              bufsize, offset + len);

  if(!duk_is_null(ctx, 4)) {
    // Seek
    fa_seek(efd->efd_fh, duk_require_number(ctx, 4), SEEK_SET);
  }

  int r = fa_read(efd->efd_fh, buf + offset, len);
  if(r < 0)
    duk_error(ctx, DUK_ERR_ERROR, "Read error from '%s'", efd->efd_path);

  duk_push_int(ctx, r);
  return 1;
}
开发者ID:Ezio-PS,项目名称:movian,代码行数:29,代码来源:es_fs.c

示例4: GetPinId

/*
 * Returns the pid id for a pin object. Also checks that the pin supports the requested function.
 */
static uint32_t GetPinId(duk_context* ctx, int idx, uint32_t function)
{
    uint32_t id;
    if (!duk_is_object(ctx, idx)) {
        duk_error(ctx, DUK_ERR_TYPE_ERROR, "Requires a pin object");
    }
    /*
     * Get pin id
     */
    duk_get_prop_string(ctx, idx, "id");
    id = duk_require_int(ctx, -1);
    duk_pop(ctx);
    /*
     * Check that the required I/O function is supported
     */
    if (function) {
        const AJS_IO_Info* info = AJS_TargetIO_GetInfo(id);
        if (!info) {
            duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Undefined I/O pin%d", id);
            return 0;
        }
        if (!(info->functions & function)) {
            duk_error(ctx, DUK_ERR_TYPE_ERROR, "I/O function %s not supported on pin%d", AJS_IO_FunctionName(function), id);
        }
    }
    return id;
}
开发者ID:avernon,项目名称:asl_distribution,代码行数:30,代码来源:ajs_io.c

示例5: es_sqlite_create

static int
es_sqlite_create(duk_context *ctx)
{
    char path[PATH_MAX];
    char errbuf[512];
    es_context_t *ec = es_get(ctx);
    const char *name = duk_safe_to_string(ctx, 0);

    // Create the db-dir for this plugin

    snprintf(path, sizeof(path), "%s/databases", ec->ec_storage);

    if(fa_makedirs(path, errbuf, sizeof(errbuf)))
        duk_error(ctx, DUK_ERR_ERROR, "Unable to create directory %s -- %s",
                  path, errbuf);

    snprintf(path, sizeof(path), "%s/databases/%s", ec->ec_storage, name);

    sqlite3 *db = db_open(path, 0);
    if(db == NULL)
        duk_error(ctx, DUK_ERR_ERROR, "Unable to open database -- check logs");


    es_sqlite_t *es = es_resource_create(ec, &es_resource_sqlite, 0);

    es->es_db = db;
    es->es_name = strdup(name);

    es_resource_push(ctx, &es->super);
    return 1;
}
开发者ID:bguerreiro,项目名称:movian,代码行数:31,代码来源:es_sqlite.c

示例6: es_get_native_obj

void *
es_get_native_obj(duk_context *ctx, int obj_idx, es_native_type_t wanted_type)
{
  duk_get_finalizer(ctx, obj_idx);

  if(!duk_is_function(ctx, -1)) {
    duk_error(ctx, DUK_ERR_ERROR, "Object is not of type %s (no finalizer)",
              native_type_to_str(wanted_type));
  }

  es_native_type_t current_type = duk_get_magic(ctx, -1);

  duk_pop(ctx);

  if(current_type != wanted_type)
    duk_error(ctx, DUK_ERR_ERROR, "Object is %s, expected %s",
              native_type_to_str(current_type),
              native_type_to_str(wanted_type));

  duk_get_prop_string(ctx, obj_idx, PTRNAME);
  if(!duk_is_pointer(ctx, -1))
    duk_error(ctx, DUK_ERR_ERROR, "Object missing ptr");

  void *r = duk_get_pointer(ctx, -1);
  duk_pop(ctx);
  return r;
}
开发者ID:peutch,项目名称:showtime,代码行数:27,代码来源:es_native_obj.c

示例7: NativePWM

static int NativePWM(duk_context* ctx)
{
    AJ_Status status;
    double dutyCycle = duk_require_number(ctx, 0);
    uint32_t freq = 0;

    if (dutyCycle > 1.0 || dutyCycle < 0.0) {
        duk_error(ctx, DUK_ERR_RANGE_ERROR, "Duty cycle must be in the range 0.0 to 1.0");
    }
    /*
     * Frequency is optional. If not specified zero is passed into the target code and the default
     * target PWM frequency is used.
     */
    if (!duk_is_undefined(ctx, 1)) {
        freq = duk_require_int(ctx, 1);
        if (freq == 0) {
            duk_error(ctx, DUK_ERR_RANGE_ERROR, "Frequency cannot be zero Hz");
        }
    }
    status = AJS_TargetIO_PinPWM(PinCtxPtr(ctx), dutyCycle, freq);
    if (status != AJ_OK) {
        if (status == AJ_ERR_RESOURCES) {
            duk_error(ctx, DUK_ERR_RANGE_ERROR, "Too many PWM pins");
        } else {
            duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Error setting PWM %s", AJ_StatusText(status));
        }
    }
    return 0;
}
开发者ID:avernon,项目名称:asl_distribution,代码行数:29,代码来源:ajs_io.c

示例8: NativeIoSpi

static int NativeIoSpi(duk_context* ctx)
{
    uint32_t mosi, miso, cs, clk, clock;
    uint8_t master;
    uint8_t cpol, cpha, data;
    void* spiCtx;
    AJ_Status status;
    int idx;

    mosi = GetPinId(ctx, 0, AJS_IO_FUNCTION_SPI_MOSI);
    miso = GetPinId(ctx, 1, AJS_IO_FUNCTION_SPI_MISO);
    cs = GetPinId(ctx, 2, AJS_IO_FUNCTION_SPI_SS);
    clk = GetPinId(ctx, 3, AJS_IO_FUNCTION_SPI_SCK);

    duk_get_prop_string(ctx, 4, "clock");
    clock = duk_require_int(ctx, -1);
    duk_get_prop_string(ctx, 4, "master");
    if (duk_get_boolean(ctx, -1)) {
        master = TRUE;
    } else {
        master = FALSE;
    }
    duk_get_prop_string(ctx, 4, "cpol");
    cpol = duk_require_int(ctx, -1);
    duk_get_prop_string(ctx, 4, "cpha");
    cpha = duk_require_int(ctx, -1);
    duk_get_prop_string(ctx, 4, "data");
    data = duk_require_int(ctx, -1);
    duk_pop(ctx);

    // Clock polarity must be high (1) or low (0)
    if (cpol != 0 && cpol != 1) {
        duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "cpol must be 0 or 1; you gave %u", cpol);
    }
    // Clock edge phase must be one edge (0) or two edge (1)
    if (cpha != 0 && cpha != 1) {
        duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "cpha must be 0 or 1; you gave %u", cpha);
    }
    // Data bits must be 8, 16, or 32
    if (data != 8 && data != 16 && data != 32) {
        duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "data bits must be 8, 16 or 32; you gave %u", data);
    }

    status = AJS_TargetIO_SpiOpen(mosi, miso, cs, clk, clock, master, cpol, cpha, data, &spiCtx);
    if (status != AJ_OK) {
        duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Failed to open SPI device");
    }
    idx = NewIOObject(ctx, spiCtx, AJS_IO_FUNCTION_SPI, NativeSpiFinalizer);

    duk_push_c_lightfunc(ctx, NativeSpiRead, 1, 0, 0);
    duk_put_prop_string(ctx, idx, "read");

    duk_push_c_lightfunc(ctx, NativeSpiWrite, 1, 0, 0);
    duk_put_prop_string(ctx, idx, "write");

    return 1;
}
开发者ID:avernon,项目名称:asl_distribution,代码行数:57,代码来源:ajs_io.c

示例9: FindInterfaceForMember

/*
 * Looks for an interface that defines a specific member.
 *
 * TODO - check for ambiguity - same member defined on multiple interfaces
 */
static const char* FindInterfaceForMember(duk_context* ctx, duk_idx_t mbrIdx, const char** member)
{
    const char* iface = NULL;
    uint8_t found = FALSE;
    size_t numInterfaces;
    duk_idx_t listIdx;

    duk_get_prop_string(ctx, -1, "interfaces");
    numInterfaces = duk_get_length(ctx, -1);
    listIdx = AJS_GetAllJoynProperty(ctx, "interfaceDefinition");

    if (duk_is_object(ctx, mbrIdx)) {
        /*
         * Expect an object of form { member:"org.foo.interface" }
         */
        duk_enum(ctx, mbrIdx, DUK_ENUM_OWN_PROPERTIES_ONLY);
        if (!duk_next(ctx, -1, 1)) {
            duk_error(ctx, DUK_ERR_TYPE_ERROR, "Require object of form { 'member-name':'interface-name' }");
        }
        iface = duk_require_string(ctx, -1);
        if (!AJ_StringFindFirstOf(iface, ".") == -1) {
            duk_error(ctx, DUK_ERR_TYPE_ERROR, "Interface name '%s' is not a dotted name", iface);
        }
        *member = duk_require_string(ctx, -2);
        duk_get_prop_string(ctx, listIdx, iface);
        if (duk_is_undefined(ctx, -1)) {
            duk_error(ctx, DUK_ERR_REFERENCE_ERROR, "Unknown interface: '%s'", iface);
        }
        found = duk_has_prop_string(ctx, -1, *member);
        duk_pop_n(ctx, 4);
    } else {
        size_t i;
        /*
         * Expect a string
         */
        *member = duk_require_string(ctx, mbrIdx);
        for (i = 0; !found && (i < numInterfaces); ++i) {
            duk_get_prop_index(ctx, -2, i);
            iface = duk_require_string(ctx, -1);
            duk_get_prop_string(ctx, listIdx, iface);
            /*
             * See if the requested member exists on this interface
             */
            found = duk_has_prop_string(ctx, -1, *member);
            duk_pop_2(ctx);
        }
    }
    duk_pop_2(ctx);
    if (!found) {
        duk_error(ctx, DUK_ERR_REFERENCE_ERROR, "Unknown member: '%s'", *member);
    }
    return iface;
}
开发者ID:anthony-ngu,项目名称:core-alljoyn-js,代码行数:58,代码来源:ajs_sessions.c

示例10: es_route_create

static int
es_route_create(duk_context *ctx)
{
  const char *str = duk_safe_to_string(ctx, 0);

  if(str[0] != '^') {
    int l = strlen(str);
    char *s = alloca(l + 2);
    s[0] = '^';
    memcpy(s+1, str, l+1);
    str = s;
  }

  es_context_t *ec = es_get(ctx);

  hts_mutex_lock(&route_mutex);

  es_route_t *er;

  LIST_FOREACH(er, &routes, er_link)
    if(!strcmp(er->er_pattern, str))
      break;

  if(er != NULL) {
    hts_mutex_unlock(&route_mutex);
    duk_error(ctx, DUK_ERR_ERROR, "Route %s already exist", str);
  }

  er = es_resource_alloc(&es_resource_route);
  if(hts_regcomp(&er->er_regex, str)) {
    hts_mutex_unlock(&route_mutex);
    free(er);
    duk_error(ctx, DUK_ERR_ERROR, "Invalid regular expression for route %s",
              str);
  }

  er->er_pattern = strdup(str);
  er->er_prio = strcspn(str, "()[].*?+$") ?: INT32_MAX;

  LIST_INSERT_SORTED(&routes, er, er_link, er_cmp, es_route_t);

  es_resource_link(&er->super, ec, 1);

  hts_mutex_unlock(&route_mutex);

  es_root_register(ctx, 1, er);

  es_resource_push(ctx, &er->super);
  return 1;
}
开发者ID:Overx,项目名称:showtime,代码行数:50,代码来源:es_route.c

示例11: dschema_check

void dschema_check(duk_context *ctx, const duv_schema_entry schema[]) {
  int i;
  int top = duk_get_top(ctx);
  for (i = 0; schema[i].name; ++i) {
    // printf("Checking arg %d-%s\n", i, schema[i].name);
    if (schema[i].checker(ctx, i)) continue;
    duk_dump_context_stderr(ctx);
    duk_error(ctx, DUK_ERR_TYPE_ERROR, "Invalid argument type for %s", schema[i].name);
  }
  if (top > i) {
    duk_dump_context_stderr(ctx);
    duk_error(ctx, DUK_ERR_TYPE_ERROR, "Too many arguments. Expected at %d, but got %d", i, top);
  }
}
开发者ID:RobertoMalatesta,项目名称:dukluv,代码行数:14,代码来源:schema.c

示例12: SerializeToBuffer

/*
 * 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;
}
开发者ID:avernon,项目名称:asl_distribution,代码行数:52,代码来源:ajs_io.c

示例13: duk_pmem

static duk_ret_t duk_pmem(duk_context* duk)
{
	tic_machine* machine = getDukMachine(duk);
	tic_mem* memory = &machine->memory;

	u32 index = duk_to_int(duk, 0);

	if(index < TIC_PERSISTENT_SIZE)
	{
		u32 val = memory->persistent.data[index];

		if(!duk_is_null_or_undefined(duk, 1))
		{
			memory->persistent.data[index] = duk_to_uint(duk, 1);
			machine->data->syncPMEM = true;
		}
		
		duk_push_int(duk, val);

		return 1;
	}
	else return duk_error(duk, DUK_ERR_ERROR, "invalid persistent memory index\n");

	return 0;
}
开发者ID:rdrpenguin04,项目名称:TIC-80,代码行数:25,代码来源:jsapi.c

示例14: duk_keyp

static s32 duk_keyp(duk_context* duk)
{
	tic_machine* machine = getDukMachine(duk);
	tic_mem* tic = &machine->memory;

	if (duk_is_null_or_undefined(duk, 0))
	{
		duk_push_boolean(duk, tic->api.keyp(tic, tic_key_unknown, -1, -1));
	}
	else
	{
		tic_key key = duk_to_int(duk, 0);

		if(key >= tic_key_escape)
		{
			return duk_error(duk, DUK_ERR_ERROR, "unknown keyboard code\n");
		}
		else
		{
			if(duk_is_null_or_undefined(duk, 1) && duk_is_null_or_undefined(duk, 2))
			{
				duk_push_boolean(duk, tic->api.keyp(tic, key, -1, -1));
			}
			else
			{
				u32 hold = duk_to_int(duk, 1);
				u32 period = duk_to_int(duk, 2);

				duk_push_boolean(duk, tic->api.keyp(tic, key, hold, period));
			}
		}
	}

	return 1;
}
开发者ID:rdrpenguin04,项目名称:TIC-80,代码行数:35,代码来源:jsapi.c

示例15: duv_loadfile

// 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);
}
开发者ID:RobertoMalatesta,项目名称:dukluv,代码行数:28,代码来源:main.c


注:本文中的duk_error函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。