Beispiel #1
0
void
_ejs_process_init(ejsval global, uint32_t argc, char **argv)
{
    _ejs_Process = _ejs_object_new (_ejs_null, &_ejs_Object_specops);
    _ejs_object_setprop (global, _ejs_atom_process, _ejs_Process);

    ejsval _argv = _ejs_array_new (argc, EJS_FALSE);
    _ejs_object_setprop (_ejs_Process, _ejs_atom_argv, _argv);

    for (int i = 0; i < argc; i ++)
        _ejs_object_setprop (_argv, NUMBER_TO_EJSVAL(i), _ejs_string_new_utf8(argv[i]));

#define OBJ_PROP(x) EJS_INSTALL_ATOM_GETTER(_ejs_Process, x, _ejs_Process_get_##x)
#define OBJ_METHOD(x) EJS_INSTALL_ATOM_FUNCTION(_ejs_Process, x, _ejs_Process_##x)

    OBJ_PROP(env);

    OBJ_METHOD(exit);

    OBJ_METHOD(chdir);
    OBJ_METHOD(cwd);

#undef OBJ_PROP
#undef OBJ_METHOD
}
Beispiel #2
0
static EJSBool
json_value_to_ejsval(JSON_Value *v, ejsval *rv)
{
    switch (json_value_get_type (v)) {
    case JSONNull:
        *rv = _ejs_null;
        return EJS_TRUE;

    case JSONString:
        *rv = _ejs_string_new_utf8 (json_value_get_string(v));
        return EJS_TRUE;

    case JSONNumber:
        *rv = NUMBER_TO_EJSVAL(json_value_get_number(v));
        return EJS_TRUE;

    case JSONObject: {
        JSON_Object *obj = json_value_get_object (v);
        *rv = _ejs_object_create (_ejs_null);

        int count = json_object_get_count (obj);
        for (int i = 0; i < count; i ++) {
            const char *propkey = json_object_get_name (obj, i);
            ejsval propval;
            if (!json_value_to_ejsval (json_object_get_value (obj, propkey), &propval))
                return EJS_FALSE;
            _ejs_object_setprop_utf8 (*rv, propkey, propval);
        }

        return EJS_TRUE;
    }
        
    case JSONArray: {
        JSON_Array *arr = json_value_get_array (v);
        int count = json_array_get_count (arr);

        *rv = _ejs_array_new (count, EJS_FALSE);

        for (int i = 0; i < count; i ++) {
            ejsval propkey = _ejs_number_new (i);
            ejsval propval;
            if (!json_value_to_ejsval (json_array_get_value (arr, i), &propval))
                return EJS_FALSE;
            _ejs_object_setprop (*rv, propkey, propval);
        }

        return EJS_TRUE;
    }
    case JSONBoolean:
        *rv = BOOLEAN_TO_EJSVAL(json_value_get_boolean(v));
        return EJS_TRUE;


    case JSONError:
        EJS_NOT_IMPLEMENTED();
        return EJS_FALSE;
    }
}
Beispiel #3
0
static EJS_NATIVE_FUNC(_ejs_IteratorWrapper_prototype_getRest) {
    EJSIteratorWrapper* iter = (EJSIteratorWrapper*)EJSVAL_TO_OBJECT(*_this);
    ejsval arr = _ejs_array_new(0, EJS_FALSE);

    while (!iter->done) {
        ejsval next_value = _ejs_IteratorWrapper_prototype_getNextValue(env, _this, 0, NULL, _ejs_undefined);
        if (!iter->done)
            _ejs_array_push_dense(arr, 1, &next_value);
    }

    return arr;
}
Beispiel #4
0
    ejsval
    Function_prototype_get_args(ejsval env, ejsval _this, int argc, ejsval *args)
    {
        Function* fun = ((Function*)EJSVAL_TO_OBJECT(_this));
        int size = fun->llvm_fun->arg_size();
        ejsval result = _ejs_array_new(0, EJS_FALSE);

        unsigned Idx = 0;
        for (llvm::Function::arg_iterator AI = fun->llvm_fun->arg_begin(); Idx != size;
             ++AI, ++Idx) {
            ejsval val = Value_new(AI);
            _ejs_array_push_dense (result, 1, &val);
        }
        return result;
    }
Beispiel #5
0
// ECMA262 25.4.4.1 Promise.all ( iterable )
static ejsval
_ejs_Promise_all (ejsval env, ejsval _this, uint32_t argc, ejsval *args)
{
    EJSBool success;

    ejsval iterable = _ejs_undefined;
    if (argc > 0) iterable = args[0];

    // 1. Let C be the this value. 
    ejsval C = _this;

    // 2. Let promiseCapability be NewPromiseCapability(C). 
    // 3. ReturnIfAbrupt(promiseCapability). 
    ejsval promiseCapability = NewPromiseCapability(C);

    // 4. Let iterator be GetIterator(iterable). 
    ejsval iterator;
    success = GetIteratorP(&iterator, iterable);

    // 5. IfAbruptRejectPromise(iterator, promiseCapability). 
    if (!success) {
        _ejs_invoke_closure(EJS_CAPABILITY_GET_REJECT(promiseCapability), _ejs_undefined, 1, &iterator);
        return EJS_CAPABILITY_GET_PROMISE(promiseCapability);
    }

    // 6. Let values be ArrayCreate(0). 
    ejsval values = _ejs_array_new(0, EJS_FALSE);

    // 7. Let remainingElementsCount be a new Record { [[value]]: 1 }. 
    int remainingElementsCount = 1;

    // 8. Let index be 0. 
    int index = 0;

    // 9. Repeat 
    while (EJS_TRUE) {
        //    a. Let next be IteratorStep(iterator). 
        ejsval next;
        success = IteratorStepP(&next, iterator);

        //    b. IfAbruptRejectPromise(next, promiseCapability). 
        if (!success) {
            _ejs_invoke_closure(EJS_CAPABILITY_GET_REJECT(promiseCapability), _ejs_undefined, 1, &next);
            return EJS_CAPABILITY_GET_PROMISE(promiseCapability);
        }
        //    c. If next is false, 
        if (EJSVAL_IS_BOOLEAN(next) && !EJSVAL_TO_BOOLEAN(next)) {
            //       i. Set remainingElementsCount.[[value]] to remainingElementsCount.[[value]] - 1. 
            remainingElementsCount --;
            //       ii. If remainingElementsCount.[[value]] is 0, 
            if (remainingElementsCount == 0) {
                //           1. Let resolveResult be the result of calling the [[Call]] internal method of promiseCapability.[[Resolve]] with undefined as thisArgument and (values) as argumentsList. 
                ejsval resolveResult;
                success = _ejs_invoke_closure_catch(&resolveResult, EJS_CAPABILITY_GET_RESOLVE(promiseCapability), _ejs_undefined, 1, &values);
                //           2. ReturnIfAbrupt(resolveResult). 
                if (!success) {
                    _ejs_invoke_closure(EJS_CAPABILITY_GET_REJECT(promiseCapability), _ejs_undefined, 1, &resolveResult);
                    return EJS_CAPABILITY_GET_PROMISE(promiseCapability);
                }
            }
            //       iii. Return promiseCapability.[[Promise]]. 
            return EJS_CAPABILITY_GET_PROMISE(promiseCapability);
        }

        //    d. Let nextValue be IteratorValue(next). 
        ejsval nextValue;
        success = IteratorValueP(&nextValue, next);
        //    e. IfAbruptRejectPromise(nextValue, promiseCapability). 
        if (!success) {
            _ejs_invoke_closure(EJS_CAPABILITY_GET_REJECT(promiseCapability), _ejs_undefined, 1, &nextValue);
            return EJS_CAPABILITY_GET_PROMISE(promiseCapability);
        }
        //    f. Let nextPromise be Invoke(C, "resolve", (nextValue)). 
        ejsval nextPromise;
        success =  _ejs_invoke_closure_catch (&nextPromise, Get(C, _ejs_atom_resolve), C, 1, &nextValue);
        
        //    g. IfAbruptRejectPromise(nextPromise, promiseCapability).
        if (!success) {
            _ejs_invoke_closure(EJS_CAPABILITY_GET_REJECT(promiseCapability), _ejs_undefined, 1, &nextPromise);
            return EJS_CAPABILITY_GET_PROMISE(promiseCapability);
        }
 
        //    h. Let resolveElement be a new built-in function object as defined in Promise.all Resolve Element Functions. 
        ejsval resolvingElement_env = _ejs_closureenv_new(6);
        ejsval resolveElement = _ejs_function_new_anon(resolvingElement_env, resolve_element);

        //    i. Set the [[AlreadyCalled]] internal slot of resolveElement to false. 
        EJS_RESOLVEELEMENT_SET_ALREADY_CALLED(resolvingElement_env, _ejs_false);
        //    j. Set the [[Index]] internal slot of resolveElement to index. 
        EJS_RESOLVEELEMENT_SET_INDEX(resolvingElement_env, NUMBER_TO_EJSVAL(index));
        //    k. Set the [[Values]] internal slot of resolveElement to values. 
        EJS_RESOLVEELEMENT_SET_VALUES(resolvingElement_env, values);
        //    l. Set the [[Capabilities]] internal slot of resolveElement to promiseCapability. 
        EJS_RESOLVEELEMENT_SET_CAPABILITIES(resolvingElement_env, promiseCapability);
        //    m. Set the [[RemainingElements]] internal slot of resolveElement to remainingElementsCount. 
        EJS_RESOLVEELEMENT_SET_REMAINING_ELEMENTS(resolvingElement_env, NUMBER_TO_EJSVAL(remainingElementsCount));
        //    n. Set remainingElementsCount.[[value]] to remainingElementsCount.[[value]] + 1. 
        remainingElementsCount++;
        //    o. Let result be Invoke(nextPromise, "then", (resolveElement, promiseCapability.[[Reject]])). 
        ejsval thenargs[] = { resolveElement, EJS_CAPABILITY_GET_REJECT(promiseCapability) };
        ejsval result;
        success =  _ejs_invoke_closure_catch (&result, Get(nextPromise, _ejs_atom_then), nextPromise, 2, thenargs);
        //    p. IfAbruptRejectPromise(result, promiseCapability). 
        if (!success) {
            _ejs_invoke_closure(EJS_CAPABILITY_GET_REJECT(promiseCapability), _ejs_undefined, 1, &result);
            return EJS_CAPABILITY_GET_PROMISE(promiseCapability);
        }
        //    q. Set index to index + 1.
        index ++;
    }
}
Beispiel #6
0
static ejsval Quote(StringifyState *state, ejsval value);
static ejsval Str(StringifyState *state, ejsval key, ejsval holder);

static ejsval
JA(StringifyState *state, ejsval value)
{
    ejsval final;

    /* 1. If stack contains value then throw a TypeError exception because the structure is cyclical. */
    /* 2. Append value to stack. */
    /* 3. Let stepback be indent. */
    ejsval stepback = state->indent;
    /* 4. Let indent be the concatenation of indent and gap. */
    state->indent = _ejs_string_concat (state->indent, state->gap);
    /* 5. Let partial be an empty List. */
    ejsval partial = _ejs_array_new (0, EJS_FALSE);

    /* 6. Let len be the result of calling the [[Get]] internal method of value with argument "length". */
    int len = EJS_ARRAY_LEN(value);

    /* 7. Let index be 0. */
    int index = 0;

    /* 8. Repeat while index < len */
    while (index < len) {
        /*    a. Let strP be the result of calling the abstract operation Str with arguments ToString(index) and value.  */
        ejsval strP = Str (state, NUMBER_TO_EJSVAL(index), value);
        /*    b. If strP is undefined */
        if (EJSVAL_IS_UNDEFINED(strP)) {
            /*       i. Append "null" to partial. */
            _ejs_array_push_dense (partial, 1, &_ejs_atom_null);
Beispiel #7
0
static ejsval
_ejs_SetIterator_prototype_next (ejsval env, ejsval _this, uint32_t argc, ejsval *args)
{
    /* 1. Let O be the this value. */
    ejsval O = _this;

    /* 2. If Type(O) is not Object, throw a TypeError exception. */
    if (!EJSVAL_IS_OBJECT(O))
        _ejs_throw_nativeerror_utf8 (EJS_TYPE_ERROR, ".next called on non-object");

    /* 3. If O does not have all of the internal slots of a Set Iterator Instance (23.2.5.3),
     * throw a TypeError exception. */
    if (!EJSVAL_IS_SETITERATOR(O))
        _ejs_throw_nativeerror_utf8 (EJS_TYPE_ERROR, ".next called on non-SetIterator instance");

    EJSSetIterator *OObj = (EJSSetIterator*)EJSVAL_TO_OBJECT(O);

    /* 4. Let s be the value of the [[IteratedSet]] internal slot of O. */
    ejsval s = OObj->iterated;

    /* 5. Let index be the value of the [[SetNextIndex]] internal slot of O. */
    uint32_t index = OObj->next_index;

    /* 6. Let itemKind be the value of the [[SetIterationKind]] internal slot of O. */
    EJSSetIteratorKind itemKind = OObj->kind;

    /* 7. If s is undefined, then return CreateIterResultObject(undefined, true). */
    if (EJSVAL_IS_UNDEFINED(s))
        return _ejs_create_iter_result (_ejs_undefined, _ejs_true);

    /* 8. Assert: s has a [[SetData]] internal slot and s has been initialized so the value of
     * [[SetData]] is not undefined. */

    /* 9. Let entries be the List that is the value of the [[SetData]] internal slot of s. */
    EJSSetValueEntry *entries = EJSVAL_TO_SET(s)->head_insert;

    /* 10. Repeat while index is less than the total number of elements of entries. The number of elements must
     * be redetermined each time this method is evaluated. */
    uint32_t i = 0;
    for (EJSSetValueEntry *entry = entries; entry; entry = entry->next_insert) {

        /* Ignore this item if is marked as empty */
        if (EJSVAL_IS_NO_ITER_VALUE_MAGIC(entry->value))
            continue;

        /* Ignore this item if we haven't reached the initial needed point/index */
        if (index > i++)
            continue;

        /* a. Let e be entries[index]. */
        ejsval e = entry->value;

        /* b. Set index to index+1; */
        index = index + 1;

        /* c. Set the [[SetNextIndex]] internal slot of O to index. */
        OObj->next_index = index;

        /* d. If e is not empty, then */
        /*  (see EJSVAL_IS_NO_ITER_VALUE_MAGIC check at the beginning of the loop */

        /*      i. If itemKind is "key+value" then, */
        if (itemKind == EJS_SET_ITER_KIND_KEYVALUE) {
            /* 1. Let result be the result of performing ArrayCreate(2). */
            /* 2. Assert: result is a new, well-formed Array object so the following operations will never fail. */
            ejsval result = _ejs_array_new (2, EJS_FALSE);

            /* 3. Call CreateDataProperty(result, "0", e) . */
            _ejs_object_setprop (result, NUMBER_TO_EJSVAL(0), e);

            /* 4. Call CreateDataProperty(result, "1", e) . */
            _ejs_object_setprop (result, NUMBER_TO_EJSVAL(1), e);

            return _ejs_create_iter_result (result, _ejs_false);
        }

        /*      ii. Return CreateIterResultObject(e, false). */
        return _ejs_create_iter_result (e, _ejs_false);
    }

    /* 11. Set the [[IteratedSet]] internal slot of O to undefined. */
    OObj->iterated = _ejs_undefined;

    /* 12. Return CreateIterResultObject(undefined, true). */
    return _ejs_create_iter_result (_ejs_undefined, _ejs_true);
}
Beispiel #8
0
void allinone_for_load_just_ensure_these_functions_and_variables_are_included_please_do_not_call()
{
    JSValueHash(_ejs_nan);
    JSValueHash(jsPositiveInfinity);
    JSValueHash(jsNegativeInfinity);
    JSValueHash(jsMax);
    JSValueHash(jsMin);
    JSValueHash(_ejs_null);
    JSValueHash(_ejs_undefined);
    JSValueHash(_ejs_true);
    JSValueHash(_ejs_false);
    JSValueHash(_ejs_one);
    JSValueHash(_ejs_zero);
    JSValueHash(_ejs_global);
    JSValueHash(_ejs_console);
    JSValueHash(_ejs_Object);
    JSValueHash(_ejs_Boolean);
    JSValueHash(_ejs_Number);
    JSValueHash(_ejs_String);
    JSValueHash(_ejs_Array);
    JSValueHash(_ejs_Function);
    JSValueHash(_ejs_Process);
    JSValueHash(_ejs_Symbol_create);
    JSValueHash(_ejs_Math);
    JSValueHash(_ejs_JSON);

    jsextern_print_tick();
    jsextern_os_msleep(0);
    jsextern_os_swap(NULL, 0, 0);
    jsextern_pcre_compile(NULL);
    jsextern_pcre_study(NULL);
    jsextern_pcre_bracketcount(NULL);
    jsextern_pcre_exec(NULL, NULL, NULL, 0, 0, NULL, 0);
    jsextern_pcre_free(NULL);
    jsextern_thread_create((void *(*)(void *))NULL, NULL);
    jsextern_thread_destroy(0);
    jsextern_mutex_create();
    jsextern_mutex_destroy(0);
    jsextern_mutex_lock(0);
    jsextern_mutex_unlock(0);
    jsextern_signal_create();
    jsextern_signal_destroy(0);
    jsextern_signal_wait(0);
    jsextern_signal_send(0);

    JSValueHash(_ejs_undefined);
    
    _ejs_eval(_ejs_undefined, _ejs_undefined, 0, NULL);

    //Object
    _ejs_object_getprop_utf8(_ejs_undefined, NULL);
    _ejs_object_setprop_utf8(_ejs_global, NULL, _ejs_undefined);
    _ejs_object_define_value_property(_ejs_undefined, _ejs_undefined, _ejs_undefined, 0);
    _ejs_object_define_getter_property(_ejs_undefined, _ejs_undefined, _ejs_undefined, 0);
    _ejs_object_define_setter_property(_ejs_undefined, _ejs_undefined, _ejs_undefined, 0);
    _ejs_Object_create(_ejs_undefined, _ejs_undefined, 0, NULL);
    _ejs_Object_getOwnPropertyNames(_ejs_undefined, _ejs_undefined, 0, NULL);
    //vtable
    _ejs_specop_get(_ejs_undefined, _ejs_undefined, _ejs_undefined);
    _ejs_specop_set(_ejs_undefined, _ejs_undefined, _ejs_undefined, _ejs_undefined);
    //option
    ToEJSBool(_ejs_undefined);
    ToDouble(_ejs_undefined);
    ToUint32(_ejs_undefined);
    _ejs_op_typeof(_ejs_undefined);
    _ejs_op_instanceof(_ejs_undefined, _ejs_undefined);
    _ejs_op_typeof_is_array(_ejs_undefined);
    _ejs_op_plusplus(_ejs_undefined, EJS_FALSE);
    _ejs_op_minusminus(_ejs_undefined, EJS_FALSE);
    _ejs_op_bitwise_xor(_ejs_undefined, _ejs_undefined);
    _ejs_op_bitwise_and(_ejs_undefined, _ejs_undefined);
    _ejs_op_bitwise_or(_ejs_undefined, _ejs_undefined);
    _ejs_op_rsh(_ejs_undefined, _ejs_undefined);
    _ejs_op_ursh(_ejs_undefined, _ejs_undefined);
    _ejs_op_lsh(_ejs_undefined, _ejs_undefined);
    _ejs_op_ulsh(_ejs_undefined, _ejs_undefined);
    _ejs_op_mod(_ejs_undefined, _ejs_undefined);
    _ejs_op_add(_ejs_undefined, _ejs_undefined);
    _ejs_op_sub(_ejs_undefined, _ejs_undefined);
    _ejs_op_mult(_ejs_undefined, _ejs_undefined);
    _ejs_op_div(_ejs_undefined, _ejs_undefined);
    _ejs_op_lt(_ejs_undefined, _ejs_undefined);
    _ejs_op_le(_ejs_undefined, _ejs_undefined);
    _ejs_op_gt(_ejs_undefined, _ejs_undefined);
    _ejs_op_ge(_ejs_undefined, _ejs_undefined);
    _ejs_op_strict_eq(_ejs_undefined, _ejs_undefined);
    _ejs_op_strict_neq(_ejs_undefined, _ejs_undefined);
    _ejs_op_eq(_ejs_undefined, _ejs_undefined);
    _ejs_op_neq(_ejs_undefined, _ejs_undefined);
    //arguments
    _ejs_arguments_new(0, NULL);

    //Array
    _ejs_array_new(jsValue32Size, false);
    //String
    _ejs_string_new_utf8_len(NULL, 0);
    //Function
    _ejs_function_new_utf8(_ejs_undefined, NULL, (EJSClosureFunc)NULL);
    _ejs_invoke_closure(_ejs_undefined, _ejs_undefined, 0, NULL);
    //RegExp
    _ejs_regexp_new_utf8(NULL, NULL);
}