Skip to content

Commit e1851f5

Browse files
committed
Import test
1 parent b8d4bb2 commit e1851f5

8 files changed

Lines changed: 115 additions & 14 deletions

File tree

rulescript/RuleScriptInterp.hx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ class RuleScriptInterp extends hscript.Interp
343343
{
344344
var t:Dynamic = RuleScript.resolveScript(path);
345345

346+
if (t != null)
347+
return t;
348+
346349
var shortPath:String = null;
347350

348351
if (StringTools.contains(path, '.'))
@@ -447,9 +450,11 @@ class RuleScriptInterp extends hscript.Interp
447450
override function cnew(cl:String, args:Array<Dynamic>):Dynamic
448451
{
449452
var c:Dynamic = Type.resolveClass(cl);
450-
if (c == null)
451-
c = resolve(cl);
452-
return Reflect.isFunction(c) ? Reflect.callMethod(null, c, args) : Type.createInstance(c, args);
453+
454+
c ??= RuleScript.resolveScript(cl);
455+
c ??= resolve(cl);
456+
457+
return Reflect.Reflect.isFunction(c) ? Reflect.callMethod(null, c, args) : c is Class ? Type.createInstance(c, args) : c;
453458
}
454459

455460
function set_errorHandler(v:haxe.Exception->Dynamic):haxe.Exception->Dynamic
@@ -552,8 +557,8 @@ class RuleScriptInterp extends hscript.Interp
552557
}
553558

554559
@:noCompletion
555-
inline public function argExpr(expr:Expr):Dynamic
560+
inline public function argExpr(e:Expr):Dynamic
556561
{
557-
return expr != null ? expr : null;
562+
return e != null ? expr(e) : null;
558563
}
559564
}

rulescript/macro/RuleScriptedClass.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ class RuleScriptedClass
146146
{
147147
__rulescript = rulescript.scriptedClass.RuleScriptedClassUtil.buildRuleScript(typeName, this);
148148

149+
$e{!strict ? macro args ??= [] : macro {}} // If args equals null
150+
149151
if (__rulescript.interp.__constructor != null)
150152
switch (rulescript.Tools.getExpr(__rulescript.interp.__constructor))
151153
{

rulescript/parsers/HxParser.hx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ class HxParser extends Parser
4141
parser.line = 1;
4242
return mode == DEFAULT ? parser.parseString(code, 'rulescript', 0) : Tools.moduleDeclsToExpr(parser.parseModule(code, 'rulescript', 0));
4343
}
44+
45+
public function parseModule(code:String):Array<ModuleDecl>
46+
{
47+
parser.line = 1;
48+
return parser.parseModule(code, 'rulescript', 0);
49+
}
4450
}
4551

4652
class HScriptParserPlus extends hscript.Parser

test/scripts/haxe/ScriptedClass.rhx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package;
22

3-
class ScriptedClass extends ScriptedClassTest
3+
class ScriptedClass extends test.ScriptedClassTest
44
{
55
public function new(customArg:Int,arg1:String)
66
{
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package importTest;
2+
3+
class Script
4+
{
5+
function helloWorld(){
6+
return 'importTest.Script: hello world';
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package importTest;
2+
3+
import importTest.Script;
4+
5+
class ScriptImportTest{
6+
function main(){
7+
trace(Script.helloWorld());
8+
9+
new ScriptedClass(1,'ScriptImportTest');
10+
}
11+
}

test/src/Main.hx

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package;
22

3+
import hscript.Expr.ModuleDecl;
4+
import hscript.Printer;
35
import rulescript.*;
46
import rulescript.parsers.*;
57
import rulescript.scriptedClass.RuleScriptedClassUtil;
8+
import sys.FileSystem;
69
import sys.io.File;
710
import test.HelloWorldAbstract;
811
import test.ScriptedClassTest;
@@ -44,8 +47,8 @@ class Main
4447
stringInterpolationTest();
4548
abstractTest();
4649
moduleTest();
47-
fileScriptTest();
4850
scriptClassesTest();
51+
fileScriptTest();
4952
}
5053
catch (e)
5154
trace(e?.details());
@@ -274,16 +277,16 @@ class Main
274277
{
275278
script.getParser(HxParser).mode = MODULE;
276279

277-
RuleScriptedClassUtil.registerRuleScriptedClass('scripted', script.getParser(HxParser).parse(File.getContent('scripts/haxe/ScriptedClass.rhx')));
278-
RuleScriptedClassUtil.registerRuleScriptedClass('scriptedStrict',
280+
RuleScriptedClassUtil.registerRuleScriptedClass('ScriptedClass', script.getParser(HxParser).parse(File.getContent('scripts/haxe/ScriptedClass.rhx')));
281+
RuleScriptedClassUtil.registerRuleScriptedClass('ScriptedClassStrict',
279282
script.getParser(HxParser).parse(File.getContent('scripts/haxe/ScriptedClassStrict.rhx')));
280283

281284
// Custom constructor can't have extra args
282285

283-
new ScriptedClassTestStrict('scriptedStrict', 'Script');
286+
new ScriptedClassTestStrict('ScriptedClass', 'Script');
284287

285288
var srcClass = new SrcClassTest<Hello<Int>, Int>('Src'),
286-
scriptClass = new ScriptedClassTest('scripted', [4, 'Script']);
289+
scriptClass = new ScriptedClassTest('ScriptedClass', [4, 'Script']);
287290
trace(srcClass.info());
288291
trace(scriptClass.info());
289292

@@ -304,7 +307,7 @@ class Main
304307
Sys.println('\n[Custom RuleScriptedClass Builder]\n');
305308

306309
var srcClass = new SrcClassTest<Hello<Int>, Int>('Src'),
307-
scriptClass = new ScriptedClassTest('haxe.ScriptedClass', [1, 'Script']);
310+
scriptClass = new ScriptedClassTest('ScriptedClass', [1, 'Script']);
308311
trace(srcClass.info());
309312
trace(scriptClass.info());
310313
}
@@ -317,7 +320,7 @@ class Main
317320

318321
rulescript.superInstance = superInstance;
319322
rulescript.interp.skipNextRestore = true;
320-
rulescript.execute(File.getContent('scripts/${typeName.replace('.', '/')}.rhx'));
323+
rulescript.execute(File.getContent('scripts/haxe/${typeName.replace('.', '/')}.rhx'));
321324
return rulescript;
322325
}
323326

@@ -329,9 +332,75 @@ class Main
329332
runFileScript('haxe/StringInterpolation.rhx');
330333

331334
script.getParser(HxParser).mode = MODULE;
332-
runFileScript('haxe/test.rhc');
335+
runFileScript('haxe/test.rhx');
333336

334337
script.variables.get('main')();
338+
339+
var old = RuleScript.resolveScript;
340+
341+
RuleScript.resolveScript = function(name:String):Dynamic
342+
{
343+
if (!FileSystem.exists('scripts/haxe/${name.replace('.', '/')}.rhx'))
344+
return null;
345+
346+
var parser = new HxParser();
347+
parser.allowAll();
348+
parser.mode = MODULE;
349+
350+
var module:Array<ModuleDecl> = parser.parseModule(File.getContent('scripts/haxe/${name.replace('.', '/')}.rhx'));
351+
352+
var newModule:Array<ModuleDecl> = [];
353+
354+
var extend:String = null;
355+
for (decl in module)
356+
{
357+
switch (decl)
358+
{
359+
case DPackage(_), DUsing(_), DImport(_):
360+
newModule.push(decl);
361+
case DClass(c):
362+
if (name.split('.').pop() == c.name)
363+
{
364+
newModule.push(decl);
365+
if (c.extend != null)
366+
{
367+
extend = new Printer().typeToString(c.extend);
368+
}
369+
}
370+
default:
371+
}
372+
}
373+
374+
var obj:Dynamic = null;
375+
376+
if (extend == null)
377+
{
378+
var script = new RuleScript();
379+
script.execute(Tools.moduleDeclsToExpr(newModule));
380+
381+
obj = {};
382+
for (key => value in script.variables)
383+
Reflect.setField(obj, key, value);
384+
}
385+
else
386+
{
387+
var cl = Type.resolveClass(extend);
388+
var f = function(args:Array<Dynamic>)
389+
{
390+
return Type.createInstance(cl, [name, args]);
391+
}
392+
393+
obj = Reflect.makeVarArgs(f);
394+
}
395+
396+
return obj;
397+
}
398+
399+
runFileScript('haxe/importTest/ScriptImportTest.rhx');
400+
401+
script.variables.get('main')();
402+
403+
RuleScript.resolveScript = old;
335404
}
336405

337406
static function luaTest()

0 commit comments

Comments
 (0)