summaryrefslogtreecommitdiff
path: root/fxjs
diff options
context:
space:
mode:
Diffstat (limited to 'fxjs')
-rw-r--r--fxjs/fxjs_v8.cpp90
-rw-r--r--fxjs/fxjs_v8.h34
-rw-r--r--fxjs/fxjs_v8_embeddertest.cpp139
3 files changed, 163 insertions, 100 deletions
diff --git a/fxjs/fxjs_v8.cpp b/fxjs/fxjs_v8.cpp
index 5ce8c47569..c96cc1f0e0 100644
--- a/fxjs/fxjs_v8.cpp
+++ b/fxjs/fxjs_v8.cpp
@@ -596,79 +596,21 @@ std::vector<CFX_WideString> CFXJS_Engine::GetObjectPropertyNames(
std::vector<CFX_WideString> result;
for (uint32_t i = 0; i < val->Length(); ++i) {
- result.push_back(ToString(val->Get(context, i).ToLocalChecked()));
+ result.push_back(ToWideString(val->Get(context, i).ToLocalChecked()));
}
return result;
}
-void CFXJS_Engine::PutObjectString(v8::Local<v8::Object> pObj,
- const CFX_WideString& wsPropertyName,
- const CFX_WideString& wsValue) {
- if (pObj.IsEmpty())
- return;
- pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName),
- WSToJSString(wsValue))
- .FromJust();
-}
-
-void CFXJS_Engine::PutObjectNumber(v8::Local<v8::Object> pObj,
- const CFX_WideString& wsPropertyName,
- int nValue) {
- if (pObj.IsEmpty())
- return;
- pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName),
- v8::Int32::New(m_isolate, nValue))
- .FromJust();
-}
-
-void CFXJS_Engine::PutObjectNumber(v8::Local<v8::Object> pObj,
- const CFX_WideString& wsPropertyName,
- float fValue) {
- if (pObj.IsEmpty())
- return;
- pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName),
- v8::Number::New(m_isolate, (double)fValue))
- .FromJust();
-}
-
-void CFXJS_Engine::PutObjectNumber(v8::Local<v8::Object> pObj,
- const CFX_WideString& wsPropertyName,
- double dValue) {
- if (pObj.IsEmpty())
- return;
- pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName),
- v8::Number::New(m_isolate, (double)dValue))
- .FromJust();
-}
-
-void CFXJS_Engine::PutObjectBoolean(v8::Local<v8::Object> pObj,
- const CFX_WideString& wsPropertyName,
- bool bValue) {
- if (pObj.IsEmpty())
- return;
- pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName),
- v8::Boolean::New(m_isolate, bValue))
- .FromJust();
-}
-
-void CFXJS_Engine::PutObjectObject(v8::Local<v8::Object> pObj,
- const CFX_WideString& wsPropertyName,
- v8::Local<v8::Object> pPut) {
+void CFXJS_Engine::PutObjectProperty(v8::Local<v8::Object> pObj,
+ const CFX_WideString& wsPropertyName,
+ v8::Local<v8::Value> pPut) {
if (pObj.IsEmpty())
return;
pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName), pPut)
.FromJust();
}
-void CFXJS_Engine::PutObjectNull(v8::Local<v8::Object> pObj,
- const CFX_WideString& wsPropertyName) {
- if (pObj.IsEmpty())
- return;
- pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName),
- v8::Local<v8::Object>())
- .FromJust();
-}
v8::Local<v8::Array> CFXJS_Engine::NewArray() {
return v8::Array::New(m_isolate);
@@ -724,8 +666,8 @@ v8::Local<v8::Value> CFXJS_Engine::NewBoolean(bool b) {
return v8::Boolean::New(m_isolate, b);
}
-v8::Local<v8::Value> CFXJS_Engine::NewString(const wchar_t* str) {
- return WSToJSString(str);
+v8::Local<v8::Value> CFXJS_Engine::NewString(const CFX_WideString& str) {
+ return WSToJSString(str.c_str());
}
v8::Local<v8::Value> CFXJS_Engine::NewNull() {
@@ -752,30 +694,30 @@ bool CFXJS_Engine::ToBoolean(v8::Local<v8::Value> pValue) {
return pValue->ToBoolean(context).ToLocalChecked()->Value();
}
-double CFXJS_Engine::ToNumber(v8::Local<v8::Value> pValue) {
+double CFXJS_Engine::ToDouble(v8::Local<v8::Value> pValue) {
if (pValue.IsEmpty())
return 0.0;
v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
return pValue->ToNumber(context).ToLocalChecked()->Value();
}
-v8::Local<v8::Object> CFXJS_Engine::ToObject(v8::Local<v8::Value> pValue) {
+CFX_WideString CFXJS_Engine::ToWideString(v8::Local<v8::Value> pValue) {
if (pValue.IsEmpty())
- return v8::Local<v8::Object>();
+ return CFX_WideString();
v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
- return pValue->ToObject(context).ToLocalChecked();
+ v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked());
+ return CFX_WideString::FromUTF8(CFX_ByteStringC(*s, s.length()));
}
-CFX_WideString CFXJS_Engine::ToString(v8::Local<v8::Value> pValue) {
- if (pValue.IsEmpty())
- return L"";
+v8::Local<v8::Object> CFXJS_Engine::ToObject(v8::Local<v8::Value> pValue) {
+ if (pValue.IsEmpty() || !pValue->IsObject())
+ return v8::Local<v8::Object>();
v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
- v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked());
- return CFX_WideString::FromUTF8(CFX_ByteStringC(*s, s.length()));
+ return pValue->ToObject(context).ToLocalChecked();
}
v8::Local<v8::Array> CFXJS_Engine::ToArray(v8::Local<v8::Value> pValue) {
- if (pValue.IsEmpty())
+ if (pValue.IsEmpty() || !pValue->IsArray())
return v8::Local<v8::Array>();
v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
diff --git a/fxjs/fxjs_v8.h b/fxjs/fxjs_v8.h
index ba729626f2..6cf3ca578e 100644
--- a/fxjs/fxjs_v8.h
+++ b/fxjs/fxjs_v8.h
@@ -176,6 +176,7 @@ class CFXJS_Engine {
v8::Local<v8::Context> NewLocalContext();
v8::Local<v8::Context> GetPersistentContext();
+ v8::Local<v8::Object> GetThisObj();
v8::Local<v8::Value> NewNull();
v8::Local<v8::Array> NewArray();
@@ -183,18 +184,18 @@ class CFXJS_Engine {
v8::Local<v8::Value> NewNumber(double number);
v8::Local<v8::Value> NewNumber(float number);
v8::Local<v8::Value> NewBoolean(bool b);
- v8::Local<v8::Value> NewString(const wchar_t* str);
+ v8::Local<v8::Value> NewString(const CFX_WideString& str);
v8::Local<v8::Date> NewDate(double d);
v8::Local<v8::Object> NewFxDynamicObj(int nObjDefnID, bool bStatic = false);
- v8::Local<v8::Object> GetThisObj();
int ToInt32(v8::Local<v8::Value> pValue);
bool ToBoolean(v8::Local<v8::Value> pValue);
- double ToNumber(v8::Local<v8::Value> pValue);
- CFX_WideString ToString(v8::Local<v8::Value> pValue);
+ double ToDouble(v8::Local<v8::Value> pValue);
+ CFX_WideString ToWideString(v8::Local<v8::Value> pValue);
v8::Local<v8::Object> ToObject(v8::Local<v8::Value> pValue);
v8::Local<v8::Array> ToArray(v8::Local<v8::Value> pValue);
+ // Arrays.
unsigned GetArrayLength(v8::Local<v8::Array> pArray);
v8::Local<v8::Value> GetArrayElement(v8::Local<v8::Array> pArray,
unsigned index);
@@ -202,31 +203,14 @@ class CFXJS_Engine {
unsigned index,
v8::Local<v8::Value> pValue);
+ // Objects.
std::vector<CFX_WideString> GetObjectPropertyNames(
v8::Local<v8::Object> pObj);
v8::Local<v8::Value> GetObjectProperty(v8::Local<v8::Object> pObj,
const CFX_WideString& PropertyName);
-
- void PutObjectString(v8::Local<v8::Object> pObj,
- const CFX_WideString& wsPropertyName,
- const CFX_WideString& wsValue);
- void PutObjectNumber(v8::Local<v8::Object> pObj,
- const CFX_WideString& PropertyName,
- int nValue);
- void PutObjectNumber(v8::Local<v8::Object> pObj,
- const CFX_WideString& PropertyName,
- float fValue);
- void PutObjectNumber(v8::Local<v8::Object> pObj,
- const CFX_WideString& PropertyName,
- double dValue);
- void PutObjectBoolean(v8::Local<v8::Object> pObj,
- const CFX_WideString& PropertyName,
- bool bValue);
- void PutObjectObject(v8::Local<v8::Object> pObj,
- const CFX_WideString& PropertyName,
- v8::Local<v8::Object> pPut);
- void PutObjectNull(v8::Local<v8::Object> pObj,
- const CFX_WideString& PropertyName);
+ void PutObjectProperty(v8::Local<v8::Object> pObj,
+ const CFX_WideString& PropertyName,
+ v8::Local<v8::Value> pValue);
// Native object binding.
void SetObjectPrivate(v8::Local<v8::Object> pObj, void* p);
diff --git a/fxjs/fxjs_v8_embeddertest.cpp b/fxjs/fxjs_v8_embeddertest.cpp
index a877a36e56..4d05a20bf9 100644
--- a/fxjs/fxjs_v8_embeddertest.cpp
+++ b/fxjs/fxjs_v8_embeddertest.cpp
@@ -28,7 +28,7 @@ class FXJSV8EmbedderTest : public JSEmbedderTest {
v8::Local<v8::Object> This = engine()->GetThisObj();
v8::Local<v8::Value> fred = engine()->GetObjectProperty(This, L"fred");
EXPECT_TRUE(fred->IsNumber());
- EXPECT_EQ(expected, engine()->ToNumber(fred));
+ EXPECT_EQ(expected, engine()->ToDouble(fred));
}
};
@@ -74,3 +74,140 @@ TEST_F(FXJSV8EmbedderTest, MultipleEngines) {
engine2.ReleaseEngine();
CheckAssignmentInCurrentContext(kExpected0);
}
+
+TEST_F(FXJSV8EmbedderTest, EmptyLocal) {
+ v8::Isolate::Scope isolate_scope(isolate());
+ v8::HandleScope handle_scope(isolate());
+ v8::Context::Scope context_scope(GetV8Context());
+
+ v8::Local<v8::Value> empty;
+ EXPECT_FALSE(engine()->ToBoolean(empty));
+ EXPECT_EQ(0, engine()->ToInt32(empty));
+ EXPECT_EQ(0.0, engine()->ToDouble(empty));
+ EXPECT_EQ(L"", engine()->ToWideString(empty));
+ EXPECT_TRUE(engine()->ToObject(empty).IsEmpty());
+ EXPECT_TRUE(engine()->ToArray(empty).IsEmpty());
+}
+
+TEST_F(FXJSV8EmbedderTest, NewNull) {
+ v8::Isolate::Scope isolate_scope(isolate());
+ v8::HandleScope handle_scope(isolate());
+ v8::Context::Scope context_scope(GetV8Context());
+
+ auto nullz = engine()->NewNull();
+ EXPECT_FALSE(engine()->ToBoolean(nullz));
+ EXPECT_EQ(0, engine()->ToInt32(nullz));
+ EXPECT_EQ(0.0, engine()->ToDouble(nullz));
+ EXPECT_EQ(L"", engine()->ToWideString(nullz));
+ EXPECT_TRUE(engine()->ToObject(nullz).IsEmpty());
+ EXPECT_TRUE(engine()->ToArray(nullz).IsEmpty());
+}
+
+TEST_F(FXJSV8EmbedderTest, NewBoolean) {
+ v8::Isolate::Scope isolate_scope(isolate());
+ v8::HandleScope handle_scope(isolate());
+ v8::Context::Scope context_scope(GetV8Context());
+
+ auto boolz = engine()->NewBoolean(true);
+ EXPECT_TRUE(engine()->ToBoolean(boolz));
+ EXPECT_EQ(1, engine()->ToInt32(boolz));
+ EXPECT_EQ(1.0, engine()->ToDouble(boolz));
+ EXPECT_EQ(L"true", engine()->ToWideString(boolz));
+ EXPECT_TRUE(engine()->ToObject(boolz).IsEmpty());
+ EXPECT_TRUE(engine()->ToArray(boolz).IsEmpty());
+}
+
+TEST_F(FXJSV8EmbedderTest, NewNumber) {
+ v8::Isolate::Scope isolate_scope(isolate());
+ v8::HandleScope handle_scope(isolate());
+ v8::Context::Scope context_scope(GetV8Context());
+
+ auto num = engine()->NewNumber(42.1);
+ EXPECT_TRUE(engine()->ToBoolean(num));
+ EXPECT_EQ(42, engine()->ToInt32(num));
+ EXPECT_EQ(42.1, engine()->ToDouble(num));
+ EXPECT_EQ(L"42.1", engine()->ToWideString(num));
+ EXPECT_TRUE(engine()->ToObject(num).IsEmpty());
+ EXPECT_TRUE(engine()->ToArray(num).IsEmpty());
+}
+
+TEST_F(FXJSV8EmbedderTest, NewString) {
+ v8::Isolate::Scope isolate_scope(isolate());
+ v8::HandleScope handle_scope(isolate());
+ v8::Context::Scope context_scope(GetV8Context());
+
+ auto str = engine()->NewString(L"123");
+ EXPECT_TRUE(engine()->ToBoolean(str));
+ EXPECT_EQ(123, engine()->ToInt32(str));
+ EXPECT_EQ(123, engine()->ToDouble(str));
+ EXPECT_EQ(L"123", engine()->ToWideString(str));
+ EXPECT_TRUE(engine()->ToObject(str).IsEmpty());
+ EXPECT_TRUE(engine()->ToArray(str).IsEmpty());
+}
+
+TEST_F(FXJSV8EmbedderTest, NewDate) {
+ v8::Isolate::Scope isolate_scope(isolate());
+ v8::HandleScope handle_scope(isolate());
+ v8::Context::Scope context_scope(GetV8Context());
+
+ auto date = engine()->NewDate(1111111111);
+ EXPECT_TRUE(engine()->ToBoolean(date));
+ EXPECT_EQ(1111111111, engine()->ToInt32(date));
+ EXPECT_EQ(1111111111.0, engine()->ToDouble(date));
+ EXPECT_NE(L"", engine()->ToWideString(date)); // exact format varies.
+ EXPECT_TRUE(engine()->ToObject(date)->IsObject());
+ EXPECT_TRUE(engine()->ToArray(date).IsEmpty());
+}
+
+TEST_F(FXJSV8EmbedderTest, NewArray) {
+ v8::Isolate::Scope isolate_scope(isolate());
+ v8::HandleScope handle_scope(isolate());
+ v8::Context::Scope context_scope(GetV8Context());
+
+ auto array = engine()->NewArray();
+ EXPECT_EQ(0u, engine()->GetArrayLength(array));
+ EXPECT_FALSE(engine()->GetArrayElement(array, 2).IsEmpty());
+ EXPECT_TRUE(engine()->GetArrayElement(array, 2)->IsUndefined());
+ EXPECT_EQ(0u, engine()->GetArrayLength(array));
+
+ engine()->PutArrayElement(array, 3, engine()->NewNumber(12));
+ EXPECT_FALSE(engine()->GetArrayElement(array, 2).IsEmpty());
+ EXPECT_TRUE(engine()->GetArrayElement(array, 2)->IsUndefined());
+ EXPECT_FALSE(engine()->GetArrayElement(array, 3).IsEmpty());
+ EXPECT_TRUE(engine()->GetArrayElement(array, 3)->IsNumber());
+ EXPECT_EQ(4u, engine()->GetArrayLength(array));
+
+ EXPECT_TRUE(engine()->ToBoolean(array));
+ EXPECT_EQ(0, engine()->ToInt32(array));
+ double d = engine()->ToDouble(array);
+ EXPECT_NE(d, d); // i.e. NaN.
+ EXPECT_EQ(L",,,12", engine()->ToWideString(array));
+ EXPECT_TRUE(engine()->ToObject(array)->IsObject());
+ EXPECT_TRUE(engine()->ToArray(array)->IsArray());
+}
+
+TEST_F(FXJSV8EmbedderTest, NewObject) {
+ v8::Isolate::Scope isolate_scope(isolate());
+ v8::HandleScope handle_scope(isolate());
+ v8::Context::Scope context_scope(GetV8Context());
+
+ auto object = engine()->NewFxDynamicObj(-1);
+ EXPECT_EQ(0u, engine()->GetObjectPropertyNames(object).size());
+ EXPECT_FALSE(engine()->GetObjectProperty(object, L"clams").IsEmpty());
+ EXPECT_TRUE(engine()->GetObjectProperty(object, L"clams")->IsUndefined());
+ EXPECT_EQ(0u, engine()->GetObjectPropertyNames(object).size());
+
+ engine()->PutObjectProperty(object, L"clams", engine()->NewNumber(12));
+ EXPECT_FALSE(engine()->GetObjectProperty(object, L"clams").IsEmpty());
+ EXPECT_TRUE(engine()->GetObjectProperty(object, L"clams")->IsNumber());
+ EXPECT_EQ(1u, engine()->GetObjectPropertyNames(object).size());
+ EXPECT_EQ(L"clams", engine()->GetObjectPropertyNames(object)[0]);
+
+ EXPECT_TRUE(engine()->ToBoolean(object));
+ EXPECT_EQ(0, engine()->ToInt32(object));
+ double d = engine()->ToDouble(object);
+ EXPECT_NE(d, d); // i.e. NaN.
+ EXPECT_EQ(L"[object Object]", engine()->ToWideString(object));
+ EXPECT_TRUE(engine()->ToObject(object)->IsObject());
+ EXPECT_TRUE(engine()->ToArray(object).IsEmpty());
+}