static int AssetDatabase_GetAssetsByImporterType(duk_context* ctx)
{
    JSVM* vm = JSVM::GetJSVM(ctx);
    ToolSystem* ts = vm->GetSubsystem<ToolSystem>();
    AssetDatabase* db = vm->GetSubsystem<AssetDatabase>();
    Project* project = ts->GetProject();

    StringHash type = duk_require_string(ctx, 0);
    String resourceType = duk_require_string(ctx, 1);

    duk_push_array(ctx);

    if (!project)
        return 1;

    PODVector<Asset*> assets;
    db->GetAssetsByImporterType(type, resourceType, assets);

    for(unsigned i = 0; i < assets.Size(); i++)
    {
        js_push_class_object_instance(ctx, assets[i], 0);
        duk_put_prop_index(ctx, -2, i);
    }

    return 1;
}
示例#2
0
文件: test.c 项目: zimbabao/duktape
static duk_ret_t cb_load_module(duk_context *ctx) {
	const char *filename;
	const char *module_id;

	module_id = duk_require_string(ctx, 0);
	duk_get_prop_string(ctx, 2, "filename");
	filename = duk_require_string(ctx, -1);

	printf("load_cb: id:'%s', filename:'%s'\n", module_id, filename);

	if (strcmp(module_id, "pig.js") == 0) {
		duk_push_sprintf(ctx, "module.exports = 'you\\'re about to get eaten by %s';",
			module_id);
	} else if (strcmp(module_id, "cow.js") == 0) {
		duk_push_string(ctx, "module.exports = require('pig');");
	} else if (strcmp(module_id, "ape.js") == 0) {
		duk_push_string(ctx, "module.exports = { module: module, __filename: __filename, wasLoaded: module.loaded };");
	} else if (strcmp(module_id, "badger.js") == 0) {
		duk_push_string(ctx, "exports.foo = 123; exports.bar = 234;");
	} else if (strcmp(module_id, "comment.js") == 0) {
		duk_push_string(ctx, "exports.foo = 123; exports.bar = 234; // comment");
	} else if (strcmp(module_id, "shebang.js") == 0) {
		duk_push_string(ctx, "#!ignored\nexports.foo = 123; exports.bar = 234;");
	} else {
		duk_error(ctx, DUK_ERR_TYPE_ERROR, "cannot find module: %s", module_id);
	}

	return 1;
}
示例#3
0
static duk_ret_t test_1(duk_context *ctx) {
	duk_set_top(ctx, 0);
	duk_push_string(ctx, "foo");
	duk_push_string(ctx, "");
	dump_string(duk_require_string(ctx, 0));
	dump_string(duk_require_string(ctx, 1));
	return 0;
}
示例#4
0
int test_1(duk_context *ctx) {
	duk_set_top(ctx, 0);
	duk_push_string(ctx, "foo");
	duk_push_string(ctx, "");
	printf("string: %s\n", duk_require_string(ctx, 0));
	printf("string: %s\n", duk_require_string(ctx, 1));
	return 0;
}
示例#5
0
static int Serializer_Write(duk_context* ctx)
{
    duk_int_t magic = duk_get_current_magic(ctx);

    duk_push_this(ctx);

    // safe cast based on type check above
    Serializer* serial = CastToSerializer(ctx, duk_get_top_index(ctx));

    duk_pop(ctx);

    if (!serial)
    {
        duk_push_boolean(ctx, 0);
        return 1;
    }

    const char* str;
    size_t length;

    IO_MAGIC_TYPE v = (IO_MAGIC_TYPE) magic;

    bool success = false;

    switch(v)
    {
        case IO_MAGIC_INT:
            success = serial->WriteInt((int) duk_require_number(ctx, 0));
            break;
        case IO_MAGIC_STRING:
             str = duk_require_string(ctx, 0);
             length = strlen(str);
             success = serial->Write(str, length);
             /*
             if (length)
             {
                buffer.Resize(length);

                for (size_t i = 0; i < length; i++)
                    buffer[i] = str[i];

                serial->WriteBuffer(buffer);
             }
             */
            break;
        case IO_MAGIC_ZEROSTRING:
            success = serial->WriteString(duk_require_string(ctx, 0));
            break;
        default:
            break;
    }

    duk_push_boolean(ctx, success ? 1 : 0);

    return 1;

}
static duk_ret_t SignalWrapper_Terrain_ComponentNameChanged_Emit(duk_context* ctx)
{
    SignalWrapper_Terrain_ComponentNameChanged* wrapper = GetThisValueObject<SignalWrapper_Terrain_ComponentNameChanged>(ctx, SignalWrapper_Terrain_ComponentNameChanged_ID);
    if (!wrapper->owner_) return 0;
    String param0 = duk_require_string(ctx, 0);
    String param1 = duk_require_string(ctx, 1);
    wrapper->signal_->Emit(param0, param1);
    return 0;
}
static duk_ret_t IAsset_SaveToFile_String_String(duk_context* ctx)
{
    int numArgs = duk_get_top(ctx);
    IAsset* thisObj = GetThisWeakObject<IAsset>(ctx);
    String filename = duk_require_string(ctx, 0);
    String serializationParameters = numArgs > 1 ? duk_require_string(ctx, 1) : "";
    bool ret = thisObj->SaveToFile(filename, serializationParameters);
    duk_push_boolean(ctx, ret);
    return 1;
}
static duk_ret_t SignalWrapper_IAssetStorage_AssetChanged_Emit(duk_context* ctx)
{
    SignalWrapper_IAssetStorage_AssetChanged* wrapper = GetThisValueObject<SignalWrapper_IAssetStorage_AssetChanged>(ctx, SignalWrapper_IAssetStorage_AssetChanged_ID);
    if (!wrapper->owner_) return 0;
    IAssetStorage* param0 = GetWeakObject<IAssetStorage>(ctx, 0);
    String param1 = duk_require_string(ctx, 1);
    String param2 = duk_require_string(ctx, 2);
    IAssetStorage::ChangeType param3 = (IAssetStorage::ChangeType)(int)duk_require_number(ctx, 3);
    wrapper->signal_->Emit(param0, param1, param2, param3);
    return 0;
}
/*
 * 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;
}
示例#10
0
文件: test.c 项目: zimbabao/duktape
static duk_ret_t cb_resolve_module(duk_context *ctx) {
	const char *module_id;
	const char *parent_id;

	module_id = duk_require_string(ctx, 0);
	parent_id = duk_require_string(ctx, 1);

	duk_push_sprintf(ctx, "%s.js", module_id);
	printf("resolve_cb: id:'%s', parent-id:'%s', resolve-to:'%s'\n",
		module_id, parent_id, duk_get_string(ctx, -1));

	return 1;
}
示例#11
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;
}
示例#12
0
gboolean
_gum_duk_parse_protection (duk_context * ctx,
                           duk_idx_t index,
                           GumPageProtection * prot)
{
  const gchar * prot_str, * ch;

  if (!duk_is_string (ctx, index))
    return FALSE;

  prot_str = duk_require_string (ctx, index);

  *prot = GUM_PAGE_NO_ACCESS;
  for (ch = prot_str; *ch != '\0'; ch++)
  {
    switch (*ch)
    {
      case 'r':
        *prot |= GUM_PAGE_READ;
        break;
      case 'w':
        *prot |= GUM_PAGE_WRITE;
        break;
      case 'x':
        *prot |= GUM_PAGE_EXECUTE;
        break;
      case '-':
        break;
      default:
        return FALSE;
    }
  }

  return TRUE;
}
示例#13
0
gboolean
_gum_duk_parse_uint64 (duk_context * ctx,
                       duk_idx_t index,
                       GumDukCore * core,
                       guint64 * u)
{
  if (duk_is_string (ctx, index))
  {
    const gchar * value_as_string, * end;
    gboolean valid;

    value_as_string = duk_require_string (ctx, index);

    if (g_str_has_prefix (value_as_string, "0x"))
    {
      *u = g_ascii_strtoull (value_as_string + 2, (gchar **) &end, 16);
      valid = end != value_as_string + 2;
    }
    else
    {
      *u = g_ascii_strtoull (value_as_string, (gchar **) &end, 10);
      valid = end != value_as_string;
    }

    return valid;
  }

  return _gum_duk_get_uint64 (ctx, index, core, u);
}
示例#14
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);
}
示例#15
0
static duk_ret_t Line_FromString_Static_string(duk_context* ctx)
{
    string str = duk_require_string(ctx, 0);
    Line ret = Line::FromString(str);
    PushValueObjectCopy<Line>(ctx, ret, Line_ID, Line_Finalizer);
    return 1;
}
static duk_ret_t IAssetTransfer_EmitAssetFailed_String(duk_context* ctx)
{
    IAssetTransfer* thisObj = GetThisWeakObject<IAssetTransfer>(ctx);
    String reason = duk_require_string(ctx, 0);
    thisObj->EmitAssetFailed(reason);
    return 0;
}
static duk_ret_t IAssetTransfer_Set_internalResourceName(duk_context* ctx)
{
    IAssetTransfer* thisObj = GetThisWeakObject<IAssetTransfer>(ctx);
    String internalResourceName = duk_require_string(ctx, 0);
    thisObj->internalResourceName = internalResourceName;
    return 0;
}
示例#18
0
static duk_ret_t Terrain_EnsureTypeNameWithoutPrefix_Static_String(duk_context* ctx)
{
    String tn = duk_require_string(ctx, 0);
    String ret = Terrain::EnsureTypeNameWithoutPrefix(tn);
    duk_push_string(ctx, ret.CString());
    return 1;
}
static duk_ret_t EntityReference_Ctor_String(duk_context* ctx)
{
    String entityName = duk_require_string(ctx, 0);
    EntityReference* newObj = new EntityReference(entityName);
    PushConstructorResult<EntityReference>(ctx, newObj, EntityReference_ID, EntityReference_Finalizer);
    return 0;
}
static duk_ret_t EntityReference_Set_ref(duk_context* ctx)
{
    EntityReference* thisObj = GetThisValueObject<EntityReference>(ctx, EntityReference_ID);
    String ref = duk_require_string(ctx, 0);
    thisObj->ref = ref;
    return 0;
}
static duk_ret_t AssetReferenceList_Ctor_String(duk_context* ctx)
{
    String preferredType = duk_require_string(ctx, 0);
    AssetReferenceList* newObj = new AssetReferenceList(preferredType);
    PushConstructorResult<AssetReferenceList>(ctx, newObj, AssetReferenceList_ID, AssetReferenceList_Finalizer);
    return 0;
}
static duk_ret_t AssetReferenceList_Set_type(duk_context* ctx)
{
    AssetReferenceList* thisObj = GetThisValueObject<AssetReferenceList>(ctx, AssetReferenceList_ID);
    String type = duk_require_string(ctx, 0);
    thisObj->type = type;
    return 0;
}
示例#23
0
static gboolean
gum_kernel_scan_context_emit_match (GumAddress address,
                                    gsize size,
                                    GumKernelScanContext * self)
{
  GumDukCore * core = self->core;
  GumDukScope scope;
  duk_context * ctx;
  gboolean proceed;

  ctx = _gum_duk_scope_enter (&scope, core);

  duk_push_heapptr (ctx, self->on_match);

  _gum_duk_push_uint64 (ctx, address, core);
  duk_push_number (ctx, size);

  proceed = TRUE;

  if (_gum_duk_scope_call (&scope, 2))
  {
    if (duk_is_string (ctx, -1))
      proceed = strcmp (duk_require_string (ctx, -1), "stop") != 0;
  }
  duk_pop (ctx);

  _gum_duk_scope_leave (&scope);

  return proceed;
}
示例#24
0
static duk_ret_t IAsset_SetDiskSource_String(duk_context* ctx)
{
    IAsset* thisObj = GetThisWeakObject<IAsset>(ctx);
    String diskSource = duk_require_string(ctx, 0);
    thisObj->SetDiskSource(diskSource);
    return 0;
}
示例#25
0
static duk_ret_t Terrain_SetName_String(duk_context* ctx)
{
    Terrain* thisObj = GetThisWeakObject<Terrain>(ctx);
    String name = duk_require_string(ctx, 0);
    thisObj->SetName(name);
    return 0;
}
static duk_ret_t IAssetTransfer_Set_assetType(duk_context* ctx)
{
    IAssetTransfer* thisObj = GetThisWeakObject<IAssetTransfer>(ctx);
    String assetType = duk_require_string(ctx, 0);
    thisObj->assetType = assetType;
    return 0;
}
static duk_ret_t IAssetStorage_TrustStateFromString_Static_String(duk_context* ctx)
{
    String s = duk_require_string(ctx, 0);
    IAssetStorage::TrustState ret = IAssetStorage::TrustStateFromString(s);
    duk_push_number(ctx, ret);
    return 1;
}
示例#28
0
static int
es_faprovider_register(duk_context *ctx)
{
  es_context_t *ec = es_get(ctx);
  const char *name = duk_require_string(ctx, 0);

  es_fap_t *ef = es_resource_alloc(&es_resource_fap);

  ef->fap.fap_opaque = ef;
  if(es_prop_is_true(ctx, 1, "cachable"))
    ef->fap.fap_flags |= FAP_ALLOW_CACHE,

  ef->fap.fap_fini  = es_fap_fini;
  ef->fap.fap_open  = es_fap_open;
  ef->fap.fap_seek  = es_fap_seek;
  ef->fap.fap_read  = es_fap_read;
  ef->fap.fap_close = es_fap_close;
  ef->fap.fap_fsize = es_fap_fsize;
  ef->fap.fap_stat  = es_fap_stat;


  ef->name = strdup(name);
  ef->fap.fap_name = ef->name;

  es_resource_retain(&ef->super); // Refcount owned by FAP
  fileaccess_register_dynamic(&ef->fap);

  es_resource_link(&ef->super, ec, 1);
  es_root_register(ctx, 1, ef);
  es_resource_push(ctx, &ef->super);
  return 1;
}
示例#29
0
// Constructor of the JS Object
duk_ret_t js_myPlugin_ctor(duk_context *ctx)
{
    slog(DEBUG,DEBUG, "Creating new object of "PLUGIN_SCOPE);

    myPluginStructure *mps = malloc(sizeof(myPluginStructure));
    mps->name = duk_require_string(ctx, 0);
    
    // Push special this binding to the function being constructed
    duk_push_this(ctx);

    // Store the underlying object
    duk_push_pointer(ctx, mps);
    duk_put_prop_string(ctx, -2, "\xff""\xff""data");

    // TODO : - if not existand create a hash_map
    //        - store structure to a hash_map('name');
    //          so that it can be reached from JS and C
    
    // Store a boolean flag to mark the object as deleted because the destructor may be called several times
    duk_push_boolean(ctx, false);
    duk_put_prop_string(ctx, -2, "\xff""\xff""deleted");

    // Store the function destructor
    duk_push_c_function(ctx, js_myPlugin_dtor, 1);
    duk_set_finalizer(ctx, -2);

    return 0;
}
示例#30
0
static duk_ret_t Avatar_SetName_String(duk_context* ctx)
{
    Avatar* thisObj = GetThisWeakObject<Avatar>(ctx);
    String name = duk_require_string(ctx, 0);
    thisObj->SetName(name);
    return 0;
}