Skip to content

Commit

Permalink
修复js传null到c#,不是c#的null而是字符串"null"的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Sep 11, 2020
1 parent 95fba85 commit f74d04b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion unity/Assets/Puerts/Src/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public JsEnv() : this(new DefaultLoader(), -1)

public JsEnv(ILoader loader, int debugPort = -1)
{
const int libVersionExpect = 2;
const int libVersionExpect = 3;
int libVersion = PuertsDLL.GetLibVersion();
if (libVersion != libVersionExpect)
{
Expand Down
5 changes: 5 additions & 0 deletions unity/general/Src/UnitTest/TestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public int Add(int a, int b)
return a + b;
}
}

public bool IsStringNull(string str)
{
return str == null;
}
}

}
17 changes: 16 additions & 1 deletion unity/general/Src/UnitTest/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,23 @@ public void NestTypeTest()
jsEnv.Dispose();

Assert.AreEqual(101, ret);
}

[Test]
public void NullString()
{
var jsEnv = new JsEnv(new TxtLoader());

bool ret = jsEnv.Eval<bool>(@"
const CS = require('csharp');
let obj = new CS.Puerts.UnitTest.DerivedClass();
obj.IsStringNull(null);
");

jsEnv.Dispose();

Assert.True(ret);
}

}
}

12 changes: 11 additions & 1 deletion unity/native_src/Src/Puerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ V8_EXPORT const char *GetStringFromValue(v8::Isolate* Isolate, v8::Value *Value,
}
else
{
if (Value->IsNullOrUndefined())
{
*Length = 0;
return nullptr;
}
auto Context = Isolate->GetCurrentContext();
auto JsEngine = FV8Utils::IsolateData<JSEngine>(Isolate);
v8::Local<v8::String> Str;
Expand Down Expand Up @@ -605,7 +610,12 @@ V8_EXPORT const char *GetStringFromResult(FResultInfo *ResultInfo, int *Length)

auto JsEngine = FV8Utils::IsolateData<JSEngine>(Isolate);
v8::Local<v8::String> Str;
if (!ResultInfo->Result.Get(Isolate)->ToString(Context).ToLocal(&Str)) return nullptr;
auto Result = ResultInfo->Result.Get(Isolate);
if (Result->IsNullOrUndefined() || !Result->ToString(Context).ToLocal(&Str))
{
*Length = 0;
return nullptr;
}
*Length = Str->Utf8Length(Isolate);
if (JsEngine->StrBuffer.size() < *Length + 1) JsEngine->StrBuffer.reserve(*Length + 1);
Str->WriteUtf8(Isolate, JsEngine->StrBuffer.data());
Expand Down

0 comments on commit f74d04b

Please sign in to comment.