Exemplo n.º 1
0
Arquivo: _time.c Projeto: Iced-Tea/sjs
/*
 * High precission time. If an optional array is given
 * it returns the difference.
 */
static duk_ret_t time_hrtime(duk_context* ctx) {
    uint64_t t = sjs_time_hrtime();
    uint64_t sec;
    uint64_t nsec;
    if (duk_is_array(ctx, 0)) {
        uint64_t t0;
        uint64_t sec0;
        uint64_t nsec0;
        duk_get_prop_index(ctx, 0, 0);
        sec0 = duk_require_uint(ctx, -1);
        duk_pop(ctx);
        duk_get_prop_index(ctx, 0, 1);
        nsec0 = duk_require_uint(ctx, -1);
        duk_pop(ctx);
        t0 = sec0 * NANOS_PER_SEC + nsec0;
        t -= t0;
    }
    sec = t / NANOS_PER_SEC;
    nsec = t % NANOS_PER_SEC;
    duk_push_array(ctx);
    duk_push_uint(ctx, sec);
    duk_put_prop_index(ctx, -2, 0);
    duk_push_uint(ctx, nsec);
    duk_put_prop_index(ctx, -2, 1);
    return 1;
}
Exemplo n.º 2
0
static int
es_gumbo_node_childs(duk_context *ctx)
{
  es_gumbo_node_t *egn = es_get_native_obj(ctx, 0, &es_native_gumbo_node);
  int all = duk_get_boolean(ctx, 1);
  const GumboNode *node = egn->node;
  duk_push_array(ctx);

  if(node->type == GUMBO_NODE_ELEMENT || node->type == GUMBO_NODE_TEMPLATE) {
    const GumboElement *e = &node->v.element;
    int num = 0;
    for(int i = 0; i < e->children.length; i++) {
      GumboNode *child = e->children.data[i];
      if(child->type == GUMBO_NODE_WHITESPACE)
        continue;
      if(!all) {
        if(child->type == GUMBO_NODE_TEXT ||
           child->type == GUMBO_NODE_CDATA ||
           child->type == GUMBO_NODE_COMMENT)
          continue;
      }
      push_gumbo_node(ctx, child, egn->output);
      duk_put_prop_index(ctx, -2, num++);
    }
  }
  return 1;
}
Exemplo n.º 3
0
void StyleContext::initFunctions(const Scene& _scene) {

    if (_scene.id == m_sceneId) {
        return;
    }
    m_sceneId = _scene.id;

    auto arr_idx = duk_push_array(m_ctx);
    int id = 0;

    for (auto& function : _scene.functions()) {
        //logMsg("compile '%s'\n", function.c_str());
        duk_push_string(m_ctx, function.c_str());
        duk_push_string(m_ctx, "");

        if (duk_pcompile(m_ctx, DUK_COMPILE_FUNCTION) == 0) {
            duk_put_prop_index(m_ctx, arr_idx, id);
        } else {
            logMsg("Error: compile failed: %s\n", duk_safe_to_string(m_ctx, -1));
            duk_pop(m_ctx);
        }
        id++;
    }

    if (!duk_put_global_string(m_ctx, FUNC_ID)) {
        logMsg("Error: 'fns' object not set\n");
    }

    DUMP("setScene - %d functions\n", id);
}
Exemplo n.º 4
0
bool StyleContext::setFunctions(const std::vector<std::string>& _functions) {

    auto arr_idx = duk_push_array(m_ctx);
    int id = 0;

    bool ok = true;

    for (auto& function : _functions) {
        duk_push_string(m_ctx, function.c_str());
        duk_push_string(m_ctx, "");

        if (duk_pcompile(m_ctx, DUK_COMPILE_FUNCTION) == 0) {
            duk_put_prop_index(m_ctx, arr_idx, id);
        } else {
            LOGW("Compile failed: %s\n%s\n---",
                 duk_safe_to_string(m_ctx, -1),
                 function.c_str());
            duk_pop(m_ctx);
            ok = false;
        }
        id++;
    }

    if (!duk_put_global_string(m_ctx, FUNC_ID)) {
        LOGE("'fns' object not set");
    }

    DUMP("setFunctions\n");
    return ok;
}
Exemplo n.º 5
0
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;
}
static duk_ret_t test_2a(duk_context *ctx, void *udata) {
	(void) udata;

	/* Test first with nothing on stack index -3. */
	duk_safe_call(ctx, test__null, NULL, 0, 1); printf("%s\n", duk_safe_to_string(ctx, 0));
	duk_pop(ctx);

	duk_set_top(ctx, 0); duk_push_undefined(ctx); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_null(ctx); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_true(ctx); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_false(ctx); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_number(ctx, 123.0); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_string(ctx, "foo\x00" "bar"); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_fixed_buffer(ctx, 16); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_pointer(ctx, NULL); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_pointer(ctx, (void *) 0xdeadbeefUL); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_object(ctx); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_array(ctx); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_c_function(ctx, dummy_func, 0); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_c_lightfunc(ctx, dummy_func, 0, 0, 0); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_eval_string(ctx, "(function dummy(){})"); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_thread(ctx); test__require_calls(ctx);

	printf("done\n");
	return 0;
}
Exemplo n.º 7
0
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;
}
Exemplo n.º 8
0
DUK_EXTERNAL void duk_push_context_dump(duk_context *ctx) {
	duk_idx_t idx;
	duk_idx_t top;

	/* We don't duk_require_stack() here now, but rely on the caller having
	 * enough space.
	 */

	top = duk_get_top(ctx);
	duk_push_array(ctx);
	for (idx = 0; idx < top; idx++) {
		duk_dup(ctx, idx);
		duk_put_prop_index(ctx, -2, idx);
	}

	/* XXX: conversion errors should not propagate outwards.
	 * Perhaps values need to be coerced individually?
	 */
	duk_bi_json_stringify_helper(ctx,
	                             duk_get_top_index(ctx),  /*idx_value*/
	                             DUK_INVALID_INDEX,  /*idx_replacer*/
	                             DUK_INVALID_INDEX,  /*idx_space*/
	                             DUK_JSON_FLAG_EXT_CUSTOM |
	                             DUK_JSON_FLAG_ASCII_ONLY |
	                             DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/);

	duk_push_sprintf(ctx, "ctx: top=%ld, stack=%s", (long) top, (const char *) duk_safe_to_string(ctx, -1));
	duk_replace(ctx, -3);  /* [ ... arr jsonx(arr) res ] -> [ ... res jsonx(arr) ] */
	duk_pop(ctx);
	DUK_ASSERT(duk_is_string(ctx, -1));
}
static int FileSystem_ScanDir(duk_context* ctx)
{
    duk_push_this(ctx);

    FileSystem* fs = js_to_class_instance<FileSystem>(ctx, -1, 0);

    if ( !duk_is_string(ctx, 0) || !duk_is_string(ctx, 1) ||
            !duk_is_number(ctx, 2) || !duk_is_boolean(ctx, 3))
    {
        duk_push_string(ctx, "FileSystem::ScanDir bad args");
        duk_throw(ctx);
    }

    const char* pathName = duk_to_string(ctx, 0);
    const char* filter = duk_to_string(ctx, 1);
    unsigned flags = duk_to_number(ctx, 2);
    bool recursive = duk_to_boolean(ctx, 3) ? true : false;

    Vector<String> result;

    fs->ScanDir(result, pathName, filter, flags, recursive);

    duk_push_array(ctx);

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

    return 1;
}
Exemplo n.º 10
0
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));
}
Exemplo n.º 11
0
void test(duk_context *ctx) {
	int i, n;

	duk_push_c_function(ctx, func, 0);

	duk_push_undefined(ctx);
	duk_push_null(ctx);
	duk_push_true(ctx);
	duk_push_false(ctx);
	duk_push_number(ctx, 123.456);
	duk_push_string(ctx, "foo");
	duk_push_object(ctx);
	duk_push_array(ctx);
	duk_push_fixed_buffer(ctx, 16);
	duk_push_pointer(ctx, (void *) 0xdeadbeef);

	n = duk_get_top(ctx);
	printf("top: %d\n", n);
	for (i = 1; i < n; i++) {
		duk_dup(ctx, 0);
		duk_dup(ctx, i);
		duk_call_method(ctx, 0);  /* [ ... func this ] -> [ ret ] */
		duk_pop(ctx);
	}
}
static int Material_GetShaderParameters(duk_context* ctx)
{
    duk_push_this(ctx);
    Material* material = js_to_class_instance<Material>(ctx, -1, 0);

    const HashMap<StringHash, MaterialShaderParameter>& params =  material->GetShaderParameters();

    duk_push_array(ctx);

    unsigned j = 0;
    for (HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = params.Begin(); i != params.End(); ++i)
    {
        duk_push_object(ctx);

        duk_push_string(ctx, i->second_.name_.CString());
        duk_put_prop_string(ctx, -2, "name");

        js_push_variant(ctx, i->second_.value_);
        duk_put_prop_string(ctx, -2, "value");

        duk_push_string(ctx, i->second_.value_.ToString().CString());
        duk_put_prop_string(ctx, -2, "valueString");

        duk_push_string(ctx, i->second_.value_.GetTypeName().CString());
        duk_put_prop_string(ctx, -2, "typeName");

        duk_push_number(ctx, (double) i->second_.value_.GetType());
        duk_put_prop_string(ctx, -2, "type");

        duk_put_prop_index(ctx, -2, j++);
    }

    return 1;
}
Exemplo n.º 13
0
static duk_ret_t
js_Font_wordWrapString(duk_context* ctx)
{
	const char* text = duk_to_string(ctx, 0);
	int         width = duk_require_int(ctx, 1);
	
	font_t*     font;
	int         num_lines;
	wraptext_t* wraptext;

	int i;

	duk_push_this(ctx);
	font = duk_require_sphere_obj(ctx, -1, "Font");
	duk_pop(ctx);
	wraptext = word_wrap_text(font, text, width);
	num_lines = get_wraptext_line_count(wraptext);
	duk_push_array(ctx);
	for (i = 0; i < num_lines; ++i) {
		duk_push_string(ctx, get_wraptext_line(wraptext, i));
		duk_put_prop_index(ctx, -2, i);
	}
	free_wraptext(wraptext);
	return 1;
}
 void OnSignal(Entity * param0, Entity * param1, const float3 & param2, const float3 & param3, float param4, float param5, bool param6)
 {
     duk_context* ctx = ctx_;
     duk_push_global_object(ctx);
     duk_get_prop_string(ctx, -1, "_OnSignal");
     duk_remove(ctx, -2);
     duk_push_number(ctx, (size_t)key_);
     duk_push_array(ctx);
     PushWeakObject(ctx, param0);
     duk_put_prop_index(ctx, -2, 0);
     PushWeakObject(ctx, param1);
     duk_put_prop_index(ctx, -2, 1);
     PushValueObjectCopy<float3>(ctx, param2, float3_ID, float3_Finalizer);
     duk_put_prop_index(ctx, -2, 2);
     PushValueObjectCopy<float3>(ctx, param3, float3_ID, float3_Finalizer);
     duk_put_prop_index(ctx, -2, 3);
     duk_push_number(ctx, param4);
     duk_put_prop_index(ctx, -2, 4);
     duk_push_number(ctx, param5);
     duk_put_prop_index(ctx, -2, 5);
     duk_push_boolean(ctx, param6);
     duk_put_prop_index(ctx, -2, 6);
     bool success = duk_pcall(ctx, 2) == 0;
     if (!success) LogError("[JavaScript] OnSignal: " + GetErrorString(ctx));
     duk_pop(ctx);
 }
Exemplo n.º 15
0
AJ_Status AJS_RegisterIO(duk_context* ctx, duk_idx_t ioIdx)
{
    duk_idx_t pinIdx;
    duk_idx_t i;
    uint16_t numPins = AJS_TargetIO_GetNumPins();

    /*
     * Create base pin protoype
     */
    pinIdx = duk_push_object(ctx);
    AJS_SetPropertyAccessors(ctx, pinIdx, "functions", NULL, NativeFunctionsGetter);
    AJS_SetPropertyAccessors(ctx, pinIdx, "info", NULL, NativeInfoGetter);
    /*
     * Create the pin objects
     */
    duk_push_array(ctx);
    for (i = 0; i < numPins; ++i) {
        AJS_CreateObjectFromPrototype(ctx, pinIdx);
        duk_push_int(ctx, i);
        duk_put_prop_string(ctx, -2, "id");
        duk_put_prop_index(ctx, -2, i);
    }
    duk_put_prop_string(ctx, ioIdx, "pin");
    duk_pop(ctx);
    /*
     * Register the native functions
     */
    AJS_PutFunctionList(ctx, ioIdx, io_native_functions, TRUE);
    /*
     * GPIO attribute constants
     */
    duk_put_number_list(ctx, ioIdx, io_constants);
    /*
     * A hidden property for keeping track of triggers
     */
    duk_push_string(ctx, AJS_HIDDEN_PROP("trigs"));
    duk_push_array(ctx);
    duk_def_prop(ctx, ioIdx, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);

    /*
     * Compact the IO object and set it on the global object
     */
    duk_compact(ctx, ioIdx);
    duk_dup(ctx, ioIdx);
    duk_put_global_string(ctx, AJS_IOObjectName);
    return AJ_OK;
}
Exemplo n.º 16
0
DUK_INTERNAL duk_ret_t duk_bi_array_prototype_slice(duk_context *ctx) {
	duk_uint32_t len;
	duk_int_t start, end;
	duk_int_t i;
	duk_uarridx_t idx;
	duk_uint32_t res_length = 0;

	/* XXX: len >= 0x80000000 won't work below because we need to be
	 * able to represent -len.
	 */
	len = duk__push_this_obj_len_u32_limited(ctx);
	duk_push_array(ctx);

	/* stack[0] = start
	 * stack[1] = end
	 * stack[2] = ToObject(this)
	 * stack[3] = ToUint32(length)
	 * stack[4] = result array
	 */

	start = duk_to_int_clamped(ctx, 0, -((duk_int_t) len), (duk_int_t) len);
	if (start < 0) {
		start = len + start;
	}
	/* XXX: could duk_is_undefined() provide defaulting undefined to 'len'
	 * (the upper limit)?
	 */
	if (duk_is_undefined(ctx, 1)) {
		end = len;
	} else {
		end = duk_to_int_clamped(ctx, 1, -((duk_int_t) len), (duk_int_t) len);
		if (end < 0) {
			end = len + end;
		}
	}
	DUK_ASSERT(start >= 0 && (duk_uint32_t) start <= len);
	DUK_ASSERT(end >= 0 && (duk_uint32_t) end <= len);

	idx = 0;
	for (i = start; i < end; i++) {
		DUK_ASSERT_TOP(ctx, 5);
		if (duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) {
			duk_xdef_prop_index_wec(ctx, 4, idx);
			res_length = idx + 1;
		} else {
			duk_pop(ctx);
		}
		idx++;
		DUK_ASSERT_TOP(ctx, 5);
	}

	duk_push_u32(ctx, res_length);
	duk_xdef_prop_stridx(ctx, 4, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);

	DUK_ASSERT_TOP(ctx, 5);
	return 1;
}
Exemplo n.º 17
0
void StyleContext::parseSceneGlobals(const YAML::Node& node, const std::string& key, int seqIndex,
                                     duk_idx_t dukObject) {

    switch(node.Type()) {
        case YAML::NodeType::Scalar:
            if (key.size() == 0) {
                duk_push_string(m_ctx, node.Scalar().c_str());
                duk_put_prop_index(m_ctx, dukObject, seqIndex);
            } else {
                auto nodeValue = node.Scalar();
                if (nodeValue.compare(0, 8, "function") == 0) {
                    duk_push_string(m_ctx, key.c_str()); // push property key
                    duk_push_string(m_ctx, nodeValue.c_str()); // push function string
                    duk_push_string(m_ctx, "");
                    if (duk_pcompile(m_ctx, DUK_COMPILE_FUNCTION) == 0) { // compile function
                        duk_put_prop(m_ctx, -3); // put {key: function()}
                    } else {
                        LOGW("Compile failed in global function: %s\n%s\n---",
                             duk_safe_to_string(m_ctx, -1),
                             nodeValue.c_str());
                        duk_pop(m_ctx); //pop error
                        duk_pop(m_ctx); //pop key
                        // push property as a string
                        duk_push_string(m_ctx, nodeValue.c_str());
                        duk_put_prop_string(m_ctx, dukObject, key.c_str());
                    }
                } else {
                    duk_push_string(m_ctx, nodeValue.c_str());
                    duk_put_prop_string(m_ctx, dukObject, key.c_str());
                }
            }
            break;
        case YAML::NodeType::Sequence:
            {
                auto seqObj = duk_push_array(m_ctx);
                for (int i = 0; i < node.size(); i++) {
                    parseSceneGlobals(node[i], "", i, seqObj);
                }
                duk_put_prop_string(m_ctx, seqObj-1, key.c_str());
                break;
            }
        case YAML::NodeType::Map:
            {
                //duk_push_string(m_ctx, key.c_str());
                auto mapObj = duk_push_object(m_ctx);
                for (auto& mapNode : node) {
                    auto itemKey = mapNode.first.Scalar();
                    parseSceneGlobals(mapNode.second, itemKey, 0, mapObj);
                }
                duk_put_prop_string(m_ctx, mapObj-1, key.c_str());
            }
        default:
            break;
    }

    return;
}
Exemplo n.º 18
0
void PushStringVector(duk_context* ctx, const Vector<String>& vector)
{
    duk_push_array(ctx);

    for (unsigned i = 0; i < vector.Size(); ++i)
    {
        duk_push_string(ctx, vector[i].CString());
        duk_put_prop_index(ctx, -2, i);
    }
}
Exemplo n.º 19
0
void test(duk_context *ctx) {
	duk_idx_t i, n;

	duk_push_undefined(ctx);
	duk_push_null(ctx);
	duk_push_boolean(ctx, 0);
	duk_push_boolean(ctx, 123);
	duk_push_number(ctx, 234);
	duk_push_string(ctx, "foo");
	duk_push_object(ctx);
	duk_push_array(ctx);
	duk_push_c_function(ctx, my_c_func, DUK_VARARGS);
	duk_push_fixed_buffer(ctx, 1024);
	duk_push_dynamic_buffer(ctx, 1024);
	duk_push_pointer(ctx, (void *) 0xdeadbeefUL);

	n = duk_get_top(ctx);
	for (i = 0; i < n + 1; i++) {  /* end on invalid index on purpose */
		duk_int_t typeval, typemask;

		typeval = duk_get_type(ctx, i);
		typemask = duk_get_type_mask(ctx, i);

		printf("stack[%ld] --> type=%ld mask=0x%08lx ",
		       (long) i, (long) typeval, (long) typemask);

		switch(duk_get_type(ctx, i)) {
		case DUK_TYPE_NONE:       printf("none"); break;
		case DUK_TYPE_UNDEFINED:  printf("undefined"); break;
		case DUK_TYPE_NULL:       printf("null"); break;
		case DUK_TYPE_BOOLEAN:    printf("boolean"); break;
		case DUK_TYPE_NUMBER:     printf("number"); break;
		case DUK_TYPE_STRING:     printf("string"); break;
		case DUK_TYPE_OBJECT:     printf("object"); break;
		case DUK_TYPE_BUFFER:     printf("buffer"); break;
		case DUK_TYPE_POINTER:    printf("pointer"); break;
		default:                  printf("unknown(%d)", (int) duk_get_type(ctx, i)); break;
		}

		printf(" bool=%d num=%lf str=%s buf-is-null=%d ptr=%p",
		       (int) duk_get_boolean(ctx, i),
		       (double) duk_get_number(ctx, i),
		       duk_get_string(ctx, i),
		       (duk_get_buffer(ctx, i, NULL) == NULL ? 1 : 0),
		       duk_get_pointer(ctx, i));

		printf(" isobj=%d isarr=%d isfunc=%d",
		       (int) duk_is_object(ctx, i),
		       (int) duk_is_array(ctx, i),
		       (int) duk_is_function(ctx, i));

		printf("\n");
	}
}
Exemplo n.º 20
0
int duk_bi_array_prototype_slice(duk_context *ctx) {
	unsigned int len;
	int start, end;
	int idx;
	int i;
	duk_uint32_t res_length = 0;

	len = duk__push_this_obj_len_u32(ctx);
	duk_push_array(ctx);

	/* stack[0] = start
	 * stack[1] = end
	 * stack[2] = ToObject(this)
	 * stack[3] = ToUint32(length)
	 * stack[4] = result array
	 */

	start = duk_to_int_clamped(ctx, 0, -len, len);  /* FIXME: does not support full 32-bit range */
	if (start < 0) {
		start = len + start;
	}
	/* FIXME: could duk_is_undefined() provide defaulting undefined to 'len'
	 * (the upper limit)?
	 */
	if (duk_is_undefined(ctx, 1)) {
		end = len;
	} else {
		end = duk_to_int_clamped(ctx, 1, -len, len);
		if (end < 0) {
			end = len + end;
		}
	}
	DUK_ASSERT(start >= 0 && (duk_uint32_t) start <= len);
	DUK_ASSERT(end >= 0 && (duk_uint32_t) end <= len);

	idx = 0;
	for (i = start; i < end; i++) {
		DUK_ASSERT_TOP(ctx, 5);
		if (duk_get_prop_index(ctx, 2, i)) {
			duk_def_prop_index(ctx, 4, idx, DUK_PROPDESC_FLAGS_WEC);
			res_length = idx + 1;
		} else {
			duk_pop(ctx);
		}
		idx++;
		DUK_ASSERT_TOP(ctx, 5);
	}

	duk_push_int(ctx, res_length);  /* FIXME */
	duk_def_prop_stridx(ctx, 4, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);

	DUK_ASSERT_TOP(ctx, 5);
	return 1;
}
Exemplo n.º 21
0
static int ncurses_getmaxyx(duk_context *ctx) {
	int row, col;

	getmaxyx(stdscr, row, col);

	duk_push_array(ctx);
	duk_push_int(ctx, row);
	duk_put_prop_index(ctx, -2, 0);
	duk_push_int(ctx, col);
	duk_put_prop_index(ctx, -2, 1);
	return 1;
}
Exemplo n.º 22
0
static int
es_gumbo_find_by_class(duk_context *ctx)
{
  es_gumbo_node_t *egn = es_get_native_obj(ctx, 0, &es_native_gumbo_node);
  const char *cls = duk_to_string(ctx, 1);
  int idx = 0;
  duk_push_array(ctx);
  char **classlist = strvec_split(cls, ' ');
  es_gumbo_find_by_class_r(egn->node, classlist, ctx, &idx, egn->output);
  strvec_free(classlist);
  return 1;
}
Exemplo n.º 23
0
 void OnSignal()
 {
     duk_context* ctx = ctx_;
     duk_push_global_object(ctx);
     duk_get_prop_string(ctx, -1, "_OnSignal");
     duk_remove(ctx, -2);
     duk_push_number(ctx, (size_t)key_);
     duk_push_array(ctx);
     bool success = duk_pcall(ctx, 2) == 0;
     if (!success) LogError("[JavaScript] OnSignal: " + GetErrorString(ctx));
     duk_pop(ctx);
 }
Exemplo n.º 24
0
/*
 * Create a global array refs in the heap stash.
 */
void
mn_ref_setup(duk_context *ctx) {
	duk_push_heap_stash(ctx);

	/* Create a new array with one `0` at index `0`. */
	duk_push_array(ctx);
	duk_push_int(ctx, 0);
	duk_put_prop_index(ctx, -2, 0);
	/* Store it as "refs" in the heap stash */
	duk_put_prop_string(ctx, -2, "refs");

	duk_pop(ctx);
}
Exemplo n.º 25
0
static int
es_gumbo_find_by_tag_name(duk_context *ctx)
{
  es_gumbo_node_t *egn = es_get_native_obj(ctx, 0, &es_native_gumbo_node);
  const char *tagstr = duk_to_string(ctx, 1);
  GumboTag tag = gumbo_tag_enum(tagstr);
  if(tag == GUMBO_TAG_UNKNOWN)
    duk_error(ctx, DUK_ERR_ERROR, "Unknown tag %s", tagstr);
  int idx = 0;
  duk_push_array(ctx);
  es_gumbo_find_by_tag_name_r(egn->node, tag, ctx, &idx, egn->output);
  return 1;
}
Exemplo n.º 26
0
static void duknode_push_argv(duk_context *ctx, int argc, const char *argv[]) {
	duk_get_global_string(ctx, "process");

	duk_idx_t arg_array_index; int i;

	arg_array_index = duk_push_array(ctx);
	for (i = 0; i < argc; i++) {
		duk_push_string(ctx, argv[i]);
		duk_put_prop_index(ctx, arg_array_index, i);
	}

	duk_put_prop_string(ctx, -2, "argv");
	duk_pop(ctx);
}
static duk_ret_t test_json_serialize_1(duk_context *ctx, void *udata) {
	unsigned char *data;
	int i;
	duk_uarridx_t arridx = 0;

	(void) udata;

	data = (unsigned char *) duk_push_dynamic_buffer(ctx, 20);
	for (i = 0; i < 20; i++) {
		data[i] = 0x40 + i;
	}

	duk_push_array(ctx);  /* index 1 */
	setup_duktape_buffer(ctx, 0);
	duk_put_prop_index(ctx, 1, arridx++);
	setup_nodejs_buffer(ctx, 0);
	duk_put_prop_index(ctx, 1, arridx++);
	setup_nodejs_buffer_slice(ctx, 0, 3, 5);
	duk_put_prop_index(ctx, 1, arridx++);
	setup_arraybuffer(ctx, 0);
	duk_put_prop_index(ctx, 1, arridx++);
	setup_typedarray(ctx, 0, "Uint8Array");
	duk_put_prop_index(ctx, 1, arridx++);
	setup_typedarray_slice(ctx, 0, "Uint8Array", 2, 6);
	duk_put_prop_index(ctx, 1, arridx++);
	setup_typedarray(ctx, 0, "Uint32Array");
	duk_put_prop_index(ctx, 1, arridx++);
	setup_typedarray_slice(ctx, 0, "Uint32Array", 4, 1);
	duk_put_prop_index(ctx, 1, arridx++);

	/* Serialize with a full backing buffer first. */

	for (i = 20; i >= 0; i--) {
		printf("resize to %d\n", i);
		duk_resize_buffer(ctx, 0, i);

		duk_dup(ctx, 1);
		duk_json_encode(ctx, -1);
		printf("%s\n", duk_to_string(ctx, -1));
		duk_pop(ctx);
		duk_eval_string(ctx, "(function (v) { print(Duktape.enc('jx', v)); })");
		duk_dup(ctx, 1);
		duk_call(ctx, 1);
		duk_pop(ctx);
	}

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
 void OnSignal(IAssetTransfer * param0, String param1)
 {
     duk_context* ctx = ctx_;
     duk_push_global_object(ctx);
     duk_get_prop_string(ctx, -1, "_OnSignal");
     duk_remove(ctx, -2);
     duk_push_number(ctx, (size_t)key_);
     duk_push_array(ctx);
     PushWeakObject(ctx, param0);
     duk_put_prop_index(ctx, -2, 0);
     duk_push_string(ctx, param1.CString());
     duk_put_prop_index(ctx, -2, 1);
     bool success = duk_pcall(ctx, 2) == 0;
     if (!success) LogError("[JavaScript] OnSignal: " + GetErrorString(ctx));
     duk_pop(ctx);
 }
Exemplo n.º 29
0
void Emerobot::InstallAPI()
{
    InstallAPI_pctl();
    InstallAPI_bitcoin();


    duk_idx_t arr_idx = duk_push_array (ctx);

    for ( int i=0; i<Argc; i++ )
    {
	duk_push_string ( ctx, Argv[i]->c_str() );
	duk_put_prop_index ( ctx, arr_idx, i );
    }

    duk_put_prop_string ( ctx, -2, "ARGV" );
}
Exemplo n.º 30
0
static int ModelImporter_GetAnimations(duk_context* ctx)
{
    duk_push_this(ctx);
    ModelImporter* importer = js_to_class_instance<ModelImporter>(ctx, -1, 0);

    PODVector<Animation*> animations;
    importer->GetAnimations(animations);

    duk_push_array(ctx);

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

    return 1;
}