Skip to content

Commit 4ba5f40

Browse files
committed
Check status
1 parent fe626f9 commit 4ba5f40

File tree

4 files changed

+40
-24
lines changed

4 files changed

+40
-24
lines changed

Runtime/Code/Luau/AirshipComponent.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,20 @@ public void Init() {
125125
context = LuauContext.Protected;
126126
}
127127

128-
// Load the component onto the thread:
129-
thread = LuauScript.LoadAndExecuteScript(gameObject, context, LuauScriptCacheMode.Cached, script, true);
130-
if (thread == IntPtr.Zero) {
131-
Debug.LogError($"Component failed to load: {script.m_path}");
128+
// Load the component onto the thread
129+
thread = LuauScript.LoadAndExecuteScript(gameObject, context, LuauScriptCacheMode.Cached, script, out var status);
130+
if (status != 0) {
131+
thread = IntPtr.Zero;
132+
if (status == 1) {
133+
Debug.LogError($"AirshipComponent constructor cannot yield: {script.m_path}");
134+
} else {
135+
Debug.LogError($"Component failed to load: {script.m_path}");
136+
}
132137
return;
133138
}
134139

135140
LuauCore.onResetInstance += OnLuauReset;
136-
141+
137142
AwakeAirshipComponent();
138143
}
139144

@@ -172,7 +177,7 @@ private void AwakeAirshipComponent() {
172177
}
173178

174179
LuauPlugin.LuauInitializeAirshipComponent(context, thread, AirshipBehaviourRootV2.GetId(gameObject), _airshipComponentId, propertyDtos);
175-
InvokeAirshipLifecycle(AirshipComponentUpdateType.AirshipAwake);
180+
InvokeAirshipLifecycle(AirshipComponentUpdateType.AirshipAwake); // TODO: Maybe move this to the actual Awake method?
176181
}
177182

178183
private void Start() {

Runtime/Code/Luau/LuauCoreUtilities.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
using UnityEngine;
66

77
public partial class LuauCore : MonoBehaviour {
8-
public int ResumeScript(LuauContext context, AirshipComponent binding) {
9-
var retValue = LuauState.FromContext(context).ResumeScript(binding);
10-
11-
return retValue;
12-
}
13-
148
public void AddThread(LuauContext context, IntPtr thread, AirshipComponent binding) {
159
LuauState.FromContext(context).AddThread(thread, binding);
1610
}

Runtime/Code/Luau/LuauPluginRaw.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,22 @@ public static void Unref(IntPtr thread, int refVal) {
206206
/// <summary>
207207
/// Pushes the value referenced by "refVal" to the top of the thread's stack.
208208
/// </summary>
209-
public static void GerRef(IntPtr thread, int refVal) {
209+
public static void GetRef(IntPtr thread, int refVal) {
210210
ThrowIfNotNullPtr(LuaGetRef(thread, refVal));
211211
}
212+
213+
#if UNITY_IPHONE
214+
[DllImport("__Internal")]
215+
#else
216+
[DllImport("LuauPlugin")]
217+
#endif
218+
private static extern IntPtr LuaGetTop(IntPtr thread, ref int top);
219+
/// <summary>
220+
/// Gets the top index of the thread's stack (which can also be seen as the stack size).
221+
/// </summary>
222+
public static int GetTop(IntPtr thread) {
223+
var top = 0;
224+
ThrowIfNotNullPtr(LuaGetTop(thread, ref top));
225+
return top;
226+
}
212227
}

Runtime/Code/Luau/LuauScript.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private void Awake() {
102102
context = LuauContext.Protected;
103103
}
104104

105-
thread = LoadAndExecuteScript(gameObject, context, LuauScriptCacheMode.NotCached, script, false);
105+
thread = LoadAndExecuteScript(gameObject, context, LuauScriptCacheMode.NotCached, script, out var status);
106106
}
107107

108108
/// <summary>
@@ -137,18 +137,20 @@ public static IntPtr LoadScript(GameObject obj, LuauContext context, LuauScriptC
137137
/// <summary>
138138
/// Execute a thread. The thread must first be created with the LoadScript function.
139139
/// </summary>
140-
public static void ExecuteScript(IntPtr thread) {
140+
public static int ExecuteScript(IntPtr thread) {
141141
// Execute the new thread. We don't need to do anything after this. If the thread errors, the error will be
142142
// outputted. If the thread yields, whoever yielded it owns responsibility for resuming it (e.g. the task
143143
// scheduler):
144-
LuauPlugin.LuauRunThread(thread);
144+
return LuauPlugin.LuauRunThread(thread);
145145
}
146146

147147
/// <summary>
148148
/// Loads and executes the given script. The executed Luau thread is returned. If the thread is a nullptr, then
149149
/// that indicates that Luau failed to load the script.
150150
/// </summary>
151-
public static IntPtr LoadAndExecuteScript(GameObject obj, LuauContext context, LuauScriptCacheMode cacheMode, AirshipScript script, bool pinThread) {
151+
public static IntPtr LoadAndExecuteScript(GameObject obj, LuauContext context, LuauScriptCacheMode cacheMode, AirshipScript script, out int status) {
152+
status = -1;
153+
152154
var thread = LoadScript(obj, context, cacheMode, script);
153155

154156
var shouldCacheValue = thread == IntPtr.Zero && cacheMode == LuauScriptCacheMode.Cached;
@@ -163,16 +165,16 @@ public static IntPtr LoadAndExecuteScript(GameObject obj, LuauContext context, L
163165
return thread;
164166
}
165167

166-
if (shouldCacheValue) {
167-
var requirePath = LuauCore.GetRequirePath(script.m_path, CleanupFilePath(script.m_path));
168-
LuauPlugin.LuauCacheModuleOnThread(thread, requirePath);
168+
if (!shouldCacheValue) {
169+
status = 0;
170+
} else {
171+
status = ExecuteScript(thread);
169172
}
170173

171-
if (pinThread) {
172-
LuauPlugin.LuauPinThread(thread);
174+
if (shouldCacheValue && status == 0) {
175+
var requirePath = LuauCore.GetRequirePath(script.m_path, CleanupFilePath(script.m_path));
176+
LuauPlugin.LuauCacheModuleOnThread(thread, requirePath);
173177
}
174-
175-
ExecuteScript(thread);
176178

177179
return thread;
178180
}

0 commit comments

Comments
 (0)