Skip to content

Commit 8b03a43

Browse files
committed
Merge pull request livecode#1764 from runrevmark/feature-lcb_module_context
[[ LCB ]] Access to current LCB module ptr
2 parents 15d1e5a + 23ad7ad commit 8b03a43

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

libscript/include/script.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ MCScriptModuleRef MCScriptRetainModule(MCScriptModuleRef module);
202202
// Release a module.
203203
void MCScriptReleaseModule(MCScriptModuleRef module);
204204

205+
// Gets the module ptr for the most recent LCB stack frame on the current thread's stack.
206+
MCScriptModuleRef MCScriptGetCurrentModule(void);
207+
205208
////////////////////////////////////////////////////////////////////////////////
206209

207210
// Create an instance of the given module. If the module is single-instance it

libscript/src/script-instance.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ void __MCScriptHandlerRelease(void *context);
4646

4747
////////////////////////////////////////////////////////////////////////////////
4848

49+
// This is the module of the most recent LCB stack frame on the current thread's
50+
// stack. It is set before and after a foreign handler call so that the native
51+
// code can get some element of context.
52+
static MCScriptModuleRef s_current_module = nil;
53+
54+
////////////////////////////////////////////////////////////////////////////////
55+
4956
bool MCScriptCreateInstanceOfModule(MCScriptModuleRef p_module, MCScriptInstanceRef& r_instance)
5057
{
5158
bool t_success;
@@ -1604,7 +1611,16 @@ static bool MCScriptPerformInvoke(MCScriptFrame*& x_frame, byte_t*& x_next_bytec
16041611
MCScriptForeignHandlerDefinition *t_foreign_handler;
16051612
t_foreign_handler = MCScriptDefinitionAsForeignHandler(p_handler);
16061613

1607-
return MCScriptPerformForeignInvoke(x_frame, p_instance, t_foreign_handler, p_arguments, p_arity);
1614+
MCScriptModuleRef t_previous_module;
1615+
t_previous_module = s_current_module;
1616+
s_current_module = x_frame -> instance -> module;
1617+
1618+
bool t_success;
1619+
t_success = MCScriptPerformForeignInvoke(x_frame, p_instance, t_foreign_handler, p_arguments, p_arity);
1620+
1621+
s_current_module = t_previous_module;
1622+
1623+
return t_success;
16081624
}
16091625

16101626
/* LOAD CHECK */ __MCScriptUnreachable__("non-handler definition passed to invoke");
@@ -2307,6 +2323,13 @@ bool MCScriptCallHandlerOfInstanceInternal(MCScriptInstanceRef self, MCScriptHan
23072323

23082324
////////////////////////////////////////////////////////////////////////////////
23092325

2326+
MCScriptModuleRef MCScriptGetCurrentModule(void)
2327+
{
2328+
return s_current_module;
2329+
}
2330+
2331+
////////////////////////////////////////////////////////////////////////////////
2332+
23102333
MCHandlerCallbacks __kMCScriptHandlerCallbacks =
23112334
{
23122335
sizeof(__MCScriptHandlerContext),

0 commit comments

Comments
 (0)