Skip to content

Commit

Permalink
fix issues in jint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtGokhan committed Feb 4, 2024
1 parent daf0223 commit d04d425
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 60 deletions.
5 changes: 2 additions & 3 deletions Runtime/Helpers/Callback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using Jint;
using Jint.Native;
using Jint.Native.Object;
using ReactUnity.Scripting;
#endif

namespace ReactUnity.Helpers
Expand Down Expand Up @@ -79,7 +78,7 @@ public object Call(params object[] args)
if (v.IsNull() || v.IsUndefined()) return null;
var res = v.Engine.Invoke(v, args);
var converted = v.Engine.TypeConverter.Convert(res, typeof(object), CultureInfo.InvariantCulture);
v.Engine.RunContinuations();
v.Engine.Advanced.ProcessTasks();
return converted;
}
else if (callback is Func<JsValue, JsValue[], JsValue> cb)
Expand All @@ -88,7 +87,7 @@ public object Call(params object[] args)
var clrf = new Jint.Runtime.Interop.ClrFunction(jintEngine, "callbackFunc", cb);
var res = jintEngine.Invoke(clrf, args);
var converted = jintEngine.TypeConverter.Convert(res, typeof(object), CultureInfo.InvariantCulture);
jintEngine.RunContinuations();
jintEngine.Advanced.ProcessTasks();
return converted;
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripting/Jint/JintEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public bool IsScriptObject(object obj)

public void Update()
{
Engine.RunContinuations();
Engine.Advanced.ProcessTasks();
}
}

Expand Down
26 changes: 0 additions & 26 deletions Runtime/Scripting/Jint/JintExtensions.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Runtime/Scripting/Jint/JintExtensions.cs.meta

This file was deleted.

30 changes: 24 additions & 6 deletions Runtime/Scripting/Jint/JintTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,39 @@ public JintTypeConverter(ReactContext context, Engine engine) : base(engine)
Context = context;
}

public override object Convert(object value, Type type, IFormatProvider formatProvider)
public override bool TryConvert(object value, Type type, IFormatProvider formatProvider, out object converted)
{

if (type == typeof(object))
{
if (value is ObjectWrapper ow) return ow.Target;
if (value is JsString js) return js.ToString();
if (value is ObjectWrapper ow)
{
converted = ow.Target;
return true;
}
if (value is JsString js)
{
converted = js.ToString();
return true;
}
}

if (type == typeof(Callback) || type == typeof(object))
{
if (value is Func<JsValue, JsValue[], JsValue> cb) return Callback.From(cb, Context);
if (value is Function oi) return Callback.From(oi, Context);
if (value is Func<JsValue, JsValue[], JsValue> cb)
{
converted = Callback.From(cb, Context);
return true;
}
if (value is Function oi)
{
converted = Callback.From(oi, Context);
return true;
}
}

return base.Convert(value, type, formatProvider);

return base.TryConvert(value, type, formatProvider, out converted);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions Tests/Editor/Renderer/HtmlComponentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public IEnumerator StyleTagShouldWorkInsideHtmlTag()

[EditorInjectableTest(Script = @"
const htmlContent = `
<script>Globals.value = 5</script>
<script>Globals.val = 5</script>
`;
export default function App() {
Expand All @@ -53,12 +53,12 @@ public IEnumerator StyleTagShouldWorkInsideHtmlTag()
public IEnumerator ScriptTagShouldWorkInsideHtmlTag()
{
yield return null;
Assert.AreEqual(5, Globals["value"]);
Assert.AreEqual(5, Globals["val"]);
}

[EditorInjectableTest(Script = @"
const htmlContent = `
<button onCustomEvent='Globals.value = 5'>Click here</button>
<button onCustomEvent='Globals.val = 5'>Click here</button>
`;
export default function App() {
Expand All @@ -72,7 +72,7 @@ public IEnumerator EventsShouldWorkInsideHtmlTag()
var button = Q("button") as ButtonComponent<Button>;
button.FireEvent("onCustomEvent", null);
yield return null;
Assert.AreEqual(5, Globals["value"]);
Assert.AreEqual(5, Globals["val"]);
}

[EditorInjectableTest(Script = @"
Expand Down Expand Up @@ -100,15 +100,15 @@ public IEnumerator HtmlCanBeSetFromSource()
}

[EditorInjectableTest(Script = @"
<button onCustomEvent='Globals.value = 5'>Click here</button>
<button onCustomEvent='Globals.val = 5'>Click here</button>
", Html = true)]
public IEnumerator HtmlRendererWorks()
{
yield return null;
var button = Q("button") as ButtonComponent<Button>;
button.FireEvent("onCustomEvent", null);
yield return null;
Assert.AreEqual(5, Globals["value"]);
Assert.AreEqual(5, Globals["val"]);
}
}
}
14 changes: 7 additions & 7 deletions Tests/Editor/Renderer/ScriptComponentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ public ScriptComponentTests(JavascriptEngineType engineType) : base(engineType)
[EditorInjectableTest(Script = BaseScript)]
public IEnumerator ScriptTagExecutesOnChange()
{
Globals["scriptContent"] = "Globals.value = 5";
Globals["scriptContent"] = "Globals.val = 5";
yield return null;

Assert.AreEqual(5, Globals["value"]);
Assert.AreEqual(5, Globals["val"]);

Globals["scriptContent"] = "Globals.value = 6";
Globals["scriptContent"] = "Globals.val= 6";
yield return null;
Assert.AreEqual(6, Globals["value"]);
Assert.AreEqual(6, Globals["val"]);
}


[EditorInjectableTest(Script = BaseScript)]
public IEnumerator ScriptTagDoesNotCrashOnError()
{
LogAssert.Expect(UnityEngine.LogType.Exception, new Regex("==="));
Globals["scriptContent"] = "Globals.value ====== 5";
Globals["scriptContent"] = "Globals.val ====== 5";
yield return null;

Globals["scriptContent"] = "Globals.value = 6";
Globals["scriptContent"] = "Globals.val = 6";
yield return null;
Assert.AreEqual(6, Globals["value"]);
Assert.AreEqual(6, Globals["val"]);
}
}
}

0 comments on commit d04d425

Please sign in to comment.