Пример #1
0
result_t Routing::append(v8::Local<v8::Object> map)
{
    v8::Local<v8::Array> ks = map->GetPropertyNames();
    int32_t len = ks->Length();
    int32_t i;
    result_t hr;

    for (i = len - 1; i >= 0; i--)
    {
        v8::Local<v8::Value> k = ks->Get(i);
        v8::Local<v8::Value> v = map->Get(k);

        obj_ptr<Handler_base> hdlr = Handler_base::getInstance(v);

        if (hdlr)
        {
            append(*v8::String::Utf8Value(k), hdlr);
            continue;
        }

        hr = append(*v8::String::Utf8Value(k), v);
        if (hr < 0)
            return hr;
    }

    return 0;
}
Пример #2
0
result_t querystring_base::stringify(v8::Local<v8::Object> obj, exlib::string sep,
    exlib::string eq, v8::Local<v8::Object> opt,
    exlib::string& retVal)
{
    StringBuffer bufs;

    v8::Local<v8::Array> ks = obj->GetPropertyNames();
    int32_t len = ks->Length();
    int32_t i;
    result_t hr;

    for (i = 0; i < len; i++) {
        exlib::string strKey, strValue, str;
        v8::Local<v8::Array> vs;
        v8::Local<v8::Value> k = ks->Get(i);
        v8::Local<v8::Value> v = obj->Get(k);

        GetArgumentValue(k, strKey);
        encoding_base::encodeURIComponent(strKey, strKey);

        hr = GetArgumentValue(v, vs, true);
        if (hr >= 0) {
            int32_t len1 = vs->Length();
            int32_t i1;

            for (i1 = 0; i1 < len1; i1++) {
                hr = GetArgumentValue(vs->Get(i1), strValue);
                if (hr < 0)
                    return hr;

                encoding_base::encodeURIComponent(strValue, strValue);

                if (bufs.size() > 0)
                    bufs.append(sep);

                bufs.append(strKey);
                bufs.append(eq);
                bufs.append(strValue);
            }
        } else {
            hr = GetArgumentValue(v, strValue);
            if (hr < 0)
                return hr;

            encoding_base::encodeURIComponent(strValue, strValue);

            if (bufs.size() > 0)
                bufs.append(sep);

            bufs.append(strKey);
            bufs.append(eq);
            bufs.append(strValue);
        }
    }

    retVal = bufs.str();

    return 0;
}
Пример #3
0
v8::Local<v8::Array> FXJS_GetObjectElementNames(v8::Isolate* pIsolate,
                                                v8::Local<v8::Object> pObj) {
  if (pObj.IsEmpty())
    return v8::Local<v8::Array>();
  v8::Local<v8::Array> val;
  if (!pObj->GetPropertyNames(pIsolate->GetCurrentContext()).ToLocal(&val))
    return v8::Local<v8::Array>();
  return val;
}
Пример #4
0
result_t HttpCollection::set(v8::Local<v8::Object> map)
{
    v8::Local<v8::Array> ks = map->GetPropertyNames();
    int32_t len = ks->Length();
    int32_t i;

    for (i = 0; i < len; i++) {
        v8::Local<v8::Value> k = ks->Get(i);
        set(*v8::String::Utf8Value(k), map->Get(k));
    }

    return 0;
}
Пример #5
0
result_t SandBox::add(v8::Local<v8::Object> mods)
{
    v8::Local<v8::Array> ks = mods->GetPropertyNames();
    int len = ks->Length();
    int i;

    for (i = 0; i < len; i++)
    {
        v8::Local<v8::Value> k = ks->Get(i);
        add(*v8::String::Utf8Value(k), mods->Get(k));
    }

    return 0;
}
Пример #6
0
inline result_t _map(LruCache *o, v8::Local<v8::Object> m,
                     result_t (LruCache::*fn)(const char *name, v8::Local<v8::Value> value))
{
    v8::Local<v8::Array> ks = m->GetPropertyNames();
    int32_t len = ks->Length();
    int32_t i;

    for (i = 0; i < len; i++)
    {
        v8::Local<v8::Value> k = ks->Get(i);
        (o->*fn)(*v8::String::Utf8Value(k), m->Get(k));
    }

    return 0;
}
Пример #7
0
Файл: Map.cpp Проект: ngot/fibjs
inline result_t _map(Map *o, v8::Local<v8::Object> m,
                     result_t (Map::*fn)(exlib::string name, Variant value))
{
    v8::Local<v8::Array> ks = m->GetPropertyNames();
    int32_t len = ks->Length();
    int32_t i;

    for (i = 0; i < len; i++)
    {
        v8::Local<v8::Value> k = ks->Get(i);
        (o->*fn)(*v8::String::Utf8Value(k), m->Get(k));
    }

    return 0;
}
Пример #8
0
result_t MongoCollection::ensureIndex(v8::Local<v8::Object> keys,
    v8::Local<v8::Object> options)
{
    obj_ptr<MongoDB> db(m_db);
    if (!db)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    exlib::string name;

    v8::Local<v8::Array> ks = keys->GetPropertyNames();
    int32_t len = (int32_t)ks->Length();
    int32_t i;

    for (i = 0; i < len; i++) {
        v8::Local<v8::Value> k = ks->Get(i);
        v8::Local<v8::Value> v = keys->Get(k);

        if (!v->IsNumber() && !v->IsNumberObject())
            return CHECK_ERROR(CALL_E_INVALIDARG);

        v8::String::Utf8Value sk(k);
        v8::String::Utf8Value sv(v);

        if (name.length())
            name += '_';

        name.append(*sk, sk.length());
        name += '_';
        name.append(*sv, sv.length());
    }

    Isolate* isolate = holder();
    v8::Local<v8::Object> idx = v8::Object::New(isolate->m_isolate);

    idx->Set(isolate->NewString("name"), isolate->NewString(name));
    idx->Set(isolate->NewString("key"), keys);
    extend(options, idx);

    v8::Local<v8::Array> idxs = v8::Array::New(isolate->m_isolate);
    idxs->Set(0, idx);

    v8::Local<v8::Object> cmd = v8::Object::New(isolate->m_isolate);
    cmd->Set(isolate->NewString("indexes"), idxs);

    v8::Local<v8::Object> retVal;
    return runCommand("createIndexes", cmd, retVal);
}
Пример #9
0
std::vector<WideString> CFX_V8::GetObjectPropertyNames(
    v8::Local<v8::Object> pObj) {
  if (pObj.IsEmpty())
    return std::vector<WideString>();

  v8::Local<v8::Array> val;
  v8::Local<v8::Context> context = m_pIsolate->GetCurrentContext();
  if (!pObj->GetPropertyNames(context).ToLocal(&val))
    return std::vector<WideString>();

  std::vector<WideString> result;
  for (uint32_t i = 0; i < val->Length(); ++i) {
    result.push_back(ToWideString(val->Get(context, i).ToLocalChecked()));
  }

  return result;
}
Пример #10
0
result_t HttpFileHandler::set_mimes(v8::Local<v8::Object> mimes)
{
    v8::Local<v8::Array> keys = mimes->GetPropertyNames();
    int32_t len = keys->Length();
    int32_t i;
    result_t hr;

    for (i = 0; i < len; i++) {
        v8::Local<v8::Value> ks = keys->Get(i);
        exlib::string v;

        hr = GetArgumentValue(mimes->Get(ks), v);
        if (hr < 0)
            return CHECK_ERROR(hr);

        m_mimes.insert(std::pair<exlib::string, exlib::string>(*v8::String::Utf8Value(ks), v));
    }

    return 0;
}
Пример #11
0
result_t RedisSortedSet::add(v8::Local<v8::Object> sms, int32_t &retVal)
{
    if (sms->IsArray())
        return CHECK_ERROR(CALL_E_INVALIDARG);

    v8::Local<v8::Array> keys = sms->GetPropertyNames();
    v8::Local<v8::Array> mss = v8::Array::New(holder()->m_isolate);

    int32_t i, n = 0;

    for (i = 0; i < (int32_t)keys->Length(); i ++)
    {
        v8::Local<v8::Value> v = keys->Get(i);

        mss->Set(n++, sms->Get(v));
        mss->Set(n++, v);
    }

    return m_rdb->doCommand("ZADD", m_key, mss, retVal);
}
Пример #12
0
inline result_t _map(HttpUploadCollection *o, v8::Local<v8::Object> m,
                     result_t (HttpUploadCollection::*fn)(const char *name, Variant value))
{
    v8::Local<v8::Array> ks = m->GetPropertyNames();
    int len = ks->Length();
    int i;
    result_t hr;

    for (i = 0; i < len; i++)
    {
        v8::Local<v8::Value> k = ks->Get(i);

        if (!k->IsNumber())
        {
            hr = (o->*fn)(*v8::String::Utf8Value(k), m->Get(k));
            if (hr < 0)
                return hr;
        }
    }

    return 0;
}
Пример #13
0
result_t Redis::_map(v8::Local<v8::Object> &map, int32_t cmd)
{
    m_subMode = 1;

    Isolate* isolate = holder();
    v8::Local<v8::Array> channels = map->GetPropertyNames();
    int32_t sz = channels->Length();
    int32_t i;
    v8::Local<v8::Array> subs = v8::Array::New(isolate->m_isolate);
    int32_t count = 0;

    for (i = 0; i < sz; i ++)
    {
        v8::Local<v8::Value> channel = channels->Get(i);
        std::string s;

        GetArgumentValue(channel, s);
        s = s_cmd[cmd][1] + s;

        v8::Local<v8::Value> value = map->Get(channel);

        if (!value->IsFunction())
            return CHECK_ERROR(CALL_E_TYPEMISMATCH);

        v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(value);

        if ((cmd & 1) ? unregsub(s, func) : regsub(s, func))
            subs->Set(count ++, channel);
    }

    if (!count)
        return 0;

    Variant v;
    return doCommand(s_cmd[cmd][0], subs, v);
}
Пример #14
0
std::string json_format(v8::Local<v8::Value> obj)
{
    StringBuffer strBuffer;

    Isolate* isolate = Isolate::current();
    QuickArray<_item> stk;
    QuickArray<v8::Local<v8::Object>> vals;
    v8::Local<v8::Value> v = obj;
    v8::Local<v8::String> mark_name = isolate->NewFromUtf8("_util_format_mark");
    int32_t padding = 0;
    const int32_t tab_size = 2;
    _item *it = NULL;

    while (true)
    {
        if (v.IsEmpty())
            strBuffer.append("undefined");
        else if (v->IsUndefined() || v->IsNull() || v->IsDate() ||
                 v->IsBoolean() || v->IsBooleanObject())
            strBuffer.append(*v8::String::Utf8Value(v));
        else if (v->IsNumber() || v->IsNumberObject())
            strBuffer.append(*v8::String::Utf8Value(v->ToNumber()));
        else if (v->IsString() || v->IsStringObject())
            string_format(strBuffer, v);
        else if (v->IsRegExp())
        {
            v8::Local<v8::RegExp> re = v8::Local<v8::RegExp>::Cast(v);
            v8::Local<v8::String> src = re->GetSource();
            v8::RegExp::Flags flgs = re->GetFlags();

            strBuffer.append('/');
            strBuffer.append(*v8::String::Utf8Value(src));
            strBuffer.append('/');

            if (flgs & v8::RegExp::kIgnoreCase)
                strBuffer.append('i');
            if (flgs & v8::RegExp::kGlobal)
                strBuffer.append('g');
            if (flgs & v8::RegExp::kMultiline)
                strBuffer.append('m');
        }
        else if (v->IsObject())
        {
            do
            {
                v8::Local<v8::Object> obj = v->ToObject();
                v8::Local<v8::Array> keys = obj->GetPropertyNames();

                if (v->IsFunction() && keys->Length() == 0)
                {
                    strBuffer.append("[Function]");
                    break;
                }

                obj_ptr<Buffer_base> buf = Buffer_base::getInstance(v);
                if (buf)
                {
                    static char hexs[] = "0123456789abcdef";
                    std::string data;
                    std::string s;
                    int32_t len, i;

                    buf->toString(data);
                    len = (int32_t)data.length();

                    s.resize(len * 3 + 8);
                    memcpy(&s[0], "<Buffer", 7);

                    for (i = 0; i < len; i ++)
                    {
                        int32_t ch = (unsigned char)data[i];

                        s[i * 3 + 7] = ' ';
                        s[i * 3 + 8] = hexs[ch >> 4];
                        s[i * 3 + 9] = hexs[ch & 0xf];
                    }

                    s[i * 3 + 7] = '>';

                    strBuffer.append(s);
                    break;
                }

                obj_ptr<Int64_base> int64Val = Int64_base::getInstance(v);
                if (int64Val)
                {
                    std::string s;
                    int64Val->toString(10, s);
                    strBuffer.append(s);
                    break;
                }

                v8::Local<v8::Value> mk = obj->GetHiddenValue(mark_name);
                if (!mk.IsEmpty())
                {
                    strBuffer.append("[Circular]");
                    break;
                }

                vals.append(obj);
                obj->SetHiddenValue(mark_name, obj);

                v8::Local<v8::Value> toArray = obj->Get(isolate->NewFromUtf8("toArray"));
                if (!IsEmpty(toArray) && toArray->IsFunction())
                {
                    v = v8::Local<v8::Function>::Cast(toArray)->Call(obj, 0, NULL);
                    obj = v->ToObject();
                }

                int32_t sz = (int32_t)stk.size();

                if (v->IsArray())
                {
                    v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(v);
                    int32_t len = array->Length();

                    if (len == 0)
                        strBuffer.append("[]");
                    else
                    {
                        if (len == 1 && v->StrictEquals(array->Get(0)))
                            strBuffer.append("[Circular]");
                        else
                        {
                            stk.resize(sz + 1);
                            it = &stk[sz];

                            it->val = v;

                            it->keys = array;
                            it->len = len;

                            strBuffer.append('[');
                            padding += tab_size;
                        }
                    }
                    break;
                }

                int32_t len = keys->Length();

                if (len == 0)
                    strBuffer.append("{}");
                else
                {
                    if (len == 1 && v->StrictEquals(obj->Get(keys->Get(0))))
                        strBuffer.append("[Circular]");
                    else
                    {
                        stk.resize(sz + 1);
                        it = &stk[sz];

                        it->val = v;

                        it->obj = obj;
                        it->keys = keys;
                        it->len = len;

                        strBuffer.append('{');
                        padding += tab_size;
                    }
                }
            }
            while (false);
        }

        if (it)
        {
            while (it && it->pos == it->len)
            {
                padding -= tab_size;
                newline(strBuffer, padding);
                strBuffer.append(it->obj.IsEmpty() ? ']' : '}');

                int32_t sz = (int32_t)stk.size();

                stk.resize(sz - 1);
                if (sz > 1)
                    it = &stk[sz - 2];
                else
                    it = NULL;
            }

            if (!it)
                break;

            if (it->pos)
                strBuffer.append(',');
            newline(strBuffer, padding);

            v = it->keys->Get(it->pos ++);

            if (!it->obj.IsEmpty())
            {
                TryCatch try_catch;

                string_format(strBuffer, v);
                strBuffer.append(": ");
                v = it->obj->Get(v);
            }
        }
        else
            break;
    }

    int32_t sz1 = (int32_t)vals.size();
    int32_t i;

    for (i = 0; i < sz1; i ++)
        vals[i]->DeleteHiddenValue(mark_name);

    return strBuffer.str();
}
Пример #15
0
 /** Get the properties of an object (and its prototype) as a comma-delimited string */
 std::string v8ObjectToString(const v8::Local<v8::Object>& o) {
     v8::Local<v8::Array> properties = o->GetPropertyNames();
     v8::String::Utf8Value str(properties);
     massert(16696 , "error converting js type to Utf8Value", *str);
     return std::string(*str, str.length());
 }