Skip to content

Commit

Permalink
core, refactor: deprecate VariantEx.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Jun 5, 2023
1 parent b47dc02 commit 3bff2de
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 107 deletions.
37 changes: 31 additions & 6 deletions fibjs/include/EventEmitter.h
Expand Up @@ -528,14 +528,24 @@ class JSTrigger {
static result_t emitter_func(AsyncEmitter* p)
{
JSFiber::EnterJsScope s;
size_t i;
size_t i, sz;
bool r;

std::vector<v8::Local<v8::Value>> argv;

argv.resize(p->m_args.size());
for (i = 0; i < p->m_args.size(); i++)
argv[i] = v8::Local<v8::Value>::New(p->m_isolate->m_isolate, p->m_args[i]);
sz = p->m_variant_args.size();
if (sz) {
argv.resize(sz);
for (i = 0; i < sz; i++)
argv[i] = p->m_variant_args[i];
} else {
sz = p->m_value_args.size();
if (sz) {
argv.resize(sz);
for (i = 0; i < sz; i++)
argv[i] = p->m_value_args[i].Get(p->m_isolate->m_isolate);
}
}

if (!p->m_obj)
p->m_obj = object_base::getInstance(p->m_o.Get(p->m_isolate->m_isolate));
Expand All @@ -554,7 +564,21 @@ class JSTrigger {
void emit(exlib::string ev, Variant* args, int32_t argCount)
{
m_ev = ev;
m_args.append((VariantEx*)args, argCount);
m_variant_args.resize(argCount);

for (int32_t i = 0; i < argCount; i++)
m_variant_args[i] = args[i];

syncCall(m_isolate, emitter_func, this);
}

void emit(exlib::string ev, v8::Local<v8::Value>* args, int32_t argCount)
{
m_ev = ev;
m_value_args.resize(argCount);

for (int32_t i = 0; i < argCount; i++)
m_value_args[i].Reset(m_isolate->m_isolate, args[i]);

syncCall(m_isolate, emitter_func, this);
}
Expand All @@ -564,7 +588,8 @@ class JSTrigger {
v8::Global<v8::Object> m_o;
obj_ptr<object_base> m_obj;
exlib::string m_ev;
QuickArray<VariantEx> m_args;
std::vector<Variant> m_variant_args;
std::vector<v8::Global<v8::Value>> m_value_args;
};

result_t emit(exlib::string ev, OptArgs args, bool& retVal)
Expand Down
2 changes: 1 addition & 1 deletion fibjs/include/HttpUploadCollection.h
Expand Up @@ -79,7 +79,7 @@ class HttpUploadCollection : public HttpCollection_base {
}

private:
typedef std::pair<exlib::string, VariantEx> pair;
typedef std::pair<exlib::string, Variant> pair;
std::vector<pair> m_map;
size_t m_count;
};
Expand Down
4 changes: 2 additions & 2 deletions fibjs/include/SimpleObject.h
Expand Up @@ -26,7 +26,7 @@ class NObject : public object_base {

public:
std::map<exlib::string, int32_t>::iterator m_pos;
VariantEx m_val;
Variant m_val;
};

public:
Expand Down Expand Up @@ -182,7 +182,7 @@ class NArray : public NObject {
}

public:
std::vector<VariantEx> m_array;
std::vector<Variant> m_array;
};

class NType : public object_base {
Expand Down
92 changes: 7 additions & 85 deletions fibjs/include/Variant.h
Expand Up @@ -63,8 +63,7 @@ class Variant {
VT_JSON,
VT_UNBOUND_ARRAY,
VT_UNBOUND_OBJECT,
VT_Type = 255,
VT_Global = 256
VT_Type = 255
};

public:
Expand Down Expand Up @@ -156,14 +155,8 @@ class Variant {
else if (_t == VT_Object && m_Val.objVal)
m_Val.objVal->Unref();
else if (_t == VT_JSValue) {
if (isGlobal()) {
v8::Global<v8::Value>& jsobj = jsValEx();
jsobj.Reset();
jsobj.~Global();
} else {
v8::Local<v8::Value>& jsobj = jsVal();
jsobj.~Local();
}
v8::Local<v8::Value>& jsobj = jsVal();
jsobj.~Local();
}

set_type(VT_Undefined);
Expand All @@ -184,12 +177,8 @@ class Variant {
if (_t == VT_Object)
return operator=(v.m_Val.objVal);

if (_t == VT_JSValue) {
if (v.isGlobal())
return operator=(v.jsValEx().Get(Isolate::current()->m_isolate));
else
return operator=(v.jsVal());
}
if (_t == VT_JSValue)
return operator=(v.jsVal());

assert(_t != VT_UNBOUND_ARRAY && _t != VT_UNBOUND_OBJECT);

Expand Down Expand Up @@ -344,24 +333,14 @@ class Variant {

void set_type(Type t)
{
m_type = (Type)(t | (m_type & VT_Global));
m_type = t;
}

bool isUndefined()
{
return type() == VT_Undefined;
}

void toGlobal()
{
m_type = (Type)(m_type | VT_Global);
}

bool isGlobal() const
{
return (m_type & VT_Global) == VT_Global;
}

size_t size() const
{
if (type() != VT_String)
Expand Down Expand Up @@ -418,7 +397,7 @@ class Variant {
return m_Val.dblVal;
return 0;
}

void parseInt(const char* str, int32_t len = -1);
void parseNumber(const char* str, int32_t len = -1);
void parseDate(const char* str, int32_t len = -1)
Expand Down Expand Up @@ -507,61 +486,4 @@ class Variant {
} m_Val;
};

class VariantEx : public Variant {

public:
VariantEx()
{
toGlobal();
}

VariantEx(const Variant& v)
{
toGlobal();
operator=(v);
}

VariantEx(const VariantEx& v)
{
toGlobal();
operator=(v);
}

VariantEx(v8::Local<v8::Value> v)
{
toGlobal();
operator=(v);
}

VariantEx(const exlib::string& v)
{
toGlobal();
operator=(v);
}

VariantEx(const char* v)
{
toGlobal();
operator=(v);
}

Variant& operator=(const VariantEx& v)
{
Variant::operator=((const Variant)v);
return *this;
}

template <typename T>
VariantEx& operator=(T v)
{
Variant::operator=(v);
return *this;
}

operator v8::Local<v8::Value>() const
{
return Variant::operator v8::Local<v8::Value>();
}
};

} /* namespace fibjs */
13 changes: 2 additions & 11 deletions fibjs/src/base/Variant.cpp
Expand Up @@ -62,11 +62,7 @@ Variant& Variant::operator=(v8::Local<v8::Value> v)
else {
set_type(VT_JSValue);

if (isGlobal()) {
new (m_Val.jsVal) v8::Global<v8::Value>();
jsValEx().Reset(Isolate::current()->m_isolate, v);
} else
new (m_Val.jsVal) v8::Local<v8::Value>(v);
new (m_Val.jsVal) v8::Local<v8::Value>(v);

return *this;
}
Expand All @@ -85,7 +81,6 @@ Variant::operator v8::Local<v8::Value>() const
return v8::Undefined(isolate->m_isolate);
case VT_Null:
case VT_Type:
case VT_Global:
return v8::Null(isolate->m_isolate);
case VT_Boolean:
return m_Val.boolVal ? v8::True(isolate->m_isolate) : v8::False(isolate->m_isolate);
Expand All @@ -107,10 +102,7 @@ Variant::operator v8::Local<v8::Value>() const
return v;
}
case VT_JSValue:
if (isGlobal())
return jsValEx().Get(isolate->m_isolate);
else
return jsVal();
return jsVal();
case VT_JSON: {
v8::Local<v8::Value> v;
json_base::decode(strVal(), v);
Expand Down Expand Up @@ -270,7 +262,6 @@ void Variant::toString(exlib::string& retVal) const
break;
case VT_Null:
case VT_Type:
case VT_Global:
retVal = "null";
break;
case VT_Boolean:
Expand Down
4 changes: 2 additions & 2 deletions fibjs/src/process/process.cpp
Expand Up @@ -448,7 +448,7 @@ result_t process_base::emitWarning(v8::Local<v8::Value> warning, v8::Local<v8::O
opts->Set(context, isolate->NewString("code"), options->Get(context, isolate->NewString("code")).FromMaybe(v8::Local<v8::Value>())).IsJust();
opts->Set(context, isolate->NewString("detail"), options->Get(context, isolate->NewString("detail")).FromMaybe(v8::Local<v8::Value>())).IsJust();

Variant v = v8::Local<v8::Value>(opts);
v8::Local<v8::Value> v = opts;
(new JSTrigger::AsyncEmitter(isolate, class_info().getModule(isolate)))->emit("warning", &v, 1);

return 0;
Expand All @@ -468,7 +468,7 @@ result_t process_base::emitWarning(v8::Local<v8::Value> warning, exlib::string t
opts->Set(context, isolate->NewString("name"), isolate->NewString(type)).IsJust();
opts->Set(context, isolate->NewString("code"), isolate->NewString(code)).IsJust();

Variant v = v8::Local<v8::Value>(opts);
v8::Local<v8::Value> v = opts;
(new JSTrigger::AsyncEmitter(isolate, class_info().getModule(isolate)))->emit("warning", &v, 1);

return 0;
Expand Down

0 comments on commit 3bff2de

Please sign in to comment.