Skip to content

Commit b0d84e2

Browse files
author
livecodeali
committed
[[ Load Extension From Data ]] Allow loading extensions from data in a variable
1 parent 2d03812 commit b0d84e2

8 files changed

Lines changed: 59 additions & 25 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Extensions can be loaded from raw data using 'load extension from data <data>' syntax

engine/src/cmds.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,12 +708,14 @@ class MCLoad : public MCStatement
708708
MCExpression *message;
709709
bool is_extension : 1;
710710
bool has_resource_path : 1;
711+
bool from_data : 1;
711712
public:
712713
MCLoad()
713714
{
714715
url = message = NULL;
715716
is_extension = false;
716717
has_resource_path = false;
718+
from_data = false;
717719
}
718720
virtual ~MCLoad();
719721
virtual Parse_stat parse(MCScriptPoint &);

engine/src/cmdsc.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,7 +2356,13 @@ Parse_stat MCLoad::parse(MCScriptPoint &sp)
23562356
MCperror->add(PE_LOAD_NOFROM, sp);
23572357
return PS_ERROR;
23582358
}
2359-
if (sp.skip_token(SP_OPEN, TT_UNDEFINED) != PS_NORMAL)
2359+
2360+
// AL-2015-11-06: [[ Load Extension From Var ]] Allow loading an extension from data in a variable
2361+
if (sp.skip_token(SP_SUGAR, TT_UNDEFINED, SG_DATA) == PS_NORMAL)
2362+
{
2363+
from_data = true;
2364+
}
2365+
else if (sp.skip_token(SP_SUGAR, TT_UNDEFINED, SG_FILE) != PS_NORMAL)
23602366
{
23612367
MCperror->add(PE_LOAD_NOFILE, sp);
23622368
return PS_ERROR;
@@ -2445,15 +2451,29 @@ void MCLoad::exec_ctxt(MCExecContext& ctxt)
24452451

24462452
if (is_extension)
24472453
{
2448-
MCAutoStringRef t_filename;
24492454
MCAutoStringRef t_resource_path;
2450-
if (!ctxt . EvalExprAsStringRef(url, EE_LOAD_BADEXTENSION, &t_filename))
2451-
return;
2455+
if (has_resource_path && !ctxt . EvalExprAsStringRef(message, EE_LOAD_BADRESOURCEPATH, &t_resource_path))
2456+
return;
2457+
2458+
// AL-2015-11-06: [[ Load Extension From Var ]] Allow loading an extension from data in a variable
2459+
if (from_data)
2460+
{
2461+
MCAutoDataRef t_data;
2462+
2463+
if (!ctxt . EvalExprAsDataRef(url, EE_LOAD_BADEXTENSION, &t_data))
2464+
return;
24522465

2453-
if (has_resource_path && !ctxt . EvalExprAsStringRef(message, EE_LOAD_BADRESOURCEPATH, &t_resource_path))
2454-
return;
2466+
MCEngineLoadExtensionFromData(ctxt, *t_data, *t_resource_path);
2467+
}
2468+
else
2469+
{
2470+
MCAutoStringRef t_filename;
2471+
2472+
if (!ctxt . EvalExprAsStringRef(url, EE_LOAD_BADEXTENSION, &t_filename))
2473+
return;
24552474

2456-
MCEngineExecLoadExtension(ctxt, *t_filename, *t_resource_path);
2475+
MCEngineExecLoadExtension(ctxt, *t_filename, *t_resource_path);
2476+
}
24572477
}
24582478
else
24592479
{

engine/src/exec-extension.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static void __rebuild_library_handler_list(void)
9595
}
9696
}
9797

98-
bool MCEngineAddExtensionFromModule(MCStringRef p_filename, MCScriptModuleRef p_module)
98+
bool MCEngineAddExtensionFromModule(MCScriptModuleRef p_module)
9999
{
100100
if (!MCScriptEnsureModuleIsUsable(p_module))
101101
{
@@ -166,20 +166,10 @@ bool MCEngineLookupResourcePathForModule(MCScriptModuleRef p_module, MCStringRef
166166
return false;
167167
}
168168

169-
void MCEngineExecLoadExtension(MCExecContext& ctxt, MCStringRef p_filename, MCStringRef p_resource_path)
169+
void MCEngineLoadExtensionFromData(MCExecContext& ctxt, MCDataRef p_extension_data, MCStringRef p_resource_path)
170170
{
171-
ctxt . SetTheResultToEmpty();
172-
173-
MCAutoStringRef t_resolved_filename;
174-
if (!MCS_resolvepath(p_filename, &t_resolved_filename))
175-
return;
176-
177-
MCAutoDataRef t_data;
178-
if (!MCS_loadbinaryfile(*t_resolved_filename, &t_data))
179-
return;
180-
181171
MCStreamRef t_stream;
182-
/* UNCHECKED */ MCMemoryInputStreamCreate(MCDataGetBytePtr(*t_data), MCDataGetLength(*t_data), t_stream);
172+
/* UNCHECKED */ MCMemoryInputStreamCreate(MCDataGetBytePtr(p_extension_data), MCDataGetLength(p_extension_data), t_stream);
183173

184174
MCScriptModuleRef t_module;
185175
if (!MCScriptCreateModuleFromStream(t_stream, t_module))
@@ -195,15 +185,30 @@ void MCEngineExecLoadExtension(MCExecContext& ctxt, MCStringRef p_filename, MCSt
195185

196186
MCValueRelease(t_stream);
197187

198-
MCEngineAddExtensionFromModule(*t_resolved_filename, t_module);
199-
if (p_resource_path != nil)
200-
MCEngineAddResourcePathForModule(t_module, p_resource_path);
188+
MCEngineAddExtensionFromModule(t_module);
189+
if (p_resource_path != nil)
190+
MCEngineAddResourcePathForModule(t_module, p_resource_path);
201191

202192
MCScriptReleaseModule(t_module);
203193

204194
return;
205195
}
206196

197+
void MCEngineExecLoadExtension(MCExecContext& ctxt, MCStringRef p_filename, MCStringRef p_resource_path)
198+
{
199+
ctxt . SetTheResultToEmpty();
200+
201+
MCAutoStringRef t_resolved_filename;
202+
if (!MCS_resolvepath(p_filename, &t_resolved_filename))
203+
return;
204+
205+
MCAutoDataRef t_data;
206+
if (!MCS_loadbinaryfile(*t_resolved_filename, &t_data))
207+
return;
208+
209+
MCEngineLoadExtensionFromData(ctxt, *t_data, p_resource_path);
210+
}
211+
207212
void MCEngineExecUnloadExtension(MCExecContext& ctxt, MCStringRef p_module_name)
208213
{
209214
MCNewAutoNameRef t_name;

engine/src/exec.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3830,6 +3830,8 @@ void MCEngineExecReturnValue(MCExecContext& ctxt, MCValueRef value);
38303830
void MCEngineExecLoadExtension(MCExecContext& ctxt, MCStringRef filename, MCStringRef resource_path);
38313831
void MCEngineExecUnloadExtension(MCExecContext& ctxt, MCStringRef filename);
38323832

3833+
void MCEngineLoadExtensionFromData(MCExecContext& ctxt, MCDataRef p_extension_data, MCStringRef p_resource_path);
3834+
38333835
void MCEngineSetCaseSensitive(MCExecContext& ctxt, bool p_value);
38343836
void MCEngineGetCaseSensitive(MCExecContext& ctxt, bool& r_value);
38353837
void MCEngineSetFormSensitive(MCExecContext& ctxt, bool p_value);

engine/src/lextable.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,7 @@ static LT sugar_table[] =
20782078
{"callback", TT_CHUNK, CT_UNDEFINED},
20792079
{"caller", TT_UNDEFINED, SG_CALLER},
20802080
{"closed", TT_UNDEFINED, SG_CLOSED},
2081+
{"data", TT_UNDEFINED, SG_DATA},
20812082
{"effects", TT_UNDEFINED, SG_EFFECTS},
20822083
{"elevated", TT_UNDEFINED, SG_ELEVATED},
20832084
{"empty", TT_CHUNK, CT_UNDEFINED},

engine/src/mode_standalone.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ bool MCStandaloneCapsuleCallback(void *p_self, const uint8_t *p_digest, MCCapsul
341341
return false;
342342
}
343343

344-
extern bool MCEngineAddExtensionFromModule(MCStringRef name, MCScriptModuleRef module);
345-
if (!MCEngineAddExtensionFromModule(MCNameGetString(MCScriptGetNameOfModule(t_module)), t_module))
344+
extern bool MCEngineAddExtensionFromModule(MCScriptModuleRef module);
345+
if (!MCEngineAddExtensionFromModule(t_module))
346346
{
347347
MCScriptReleaseModule(t_module);
348348
return false;

engine/src/parsedef.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,6 +1904,9 @@ enum Sugar_constants {
19041904
SG_EXTENSION,
19051905
SG_RESOURCE,
19061906
SG_PATH,
1907+
1908+
// AL-2015-06-11: [[ Load Extension From Var ]] Add 'data' syntactic sugar
1909+
SG_DATA,
19071910
};
19081911

19091912
enum Statements {

0 commit comments

Comments
 (0)