Skip to content
Browse files

Revise downcall protocol (ki9a++) and use downcalls to invoke CLRBackend

  • Loading branch information...
1 parent 17e3722 commit b94a6274704e398ceaaad742e1649a3241e3588a @sorear committed Feb 28, 2011
Showing with 81 additions and 38 deletions.
  1. +1 −1 FETCH_URL
  2. +10 −6 Makefile
  3. +20 −16 lib/Builtins.cs
  4. +12 −7 lib/CLRBackend.cs
  5. +9 −0 lib/CrossDomainReceiver.cs
  6. +20 −1 src/NieczaBackendDotnet.pm6
  7. +8 −0 src/NieczaBackendNAM.pm6
  8. +1 −7 src/NieczaCompiler.pm6
View
2 FETCH_URL
@@ -1 +1 @@
-https://github.com/downloads/sorear/niecza/niecza-3.zip
+https://github.com/downloads/sorear/niecza/niecza-3.01.zip
View
16 Makefile
@@ -8,6 +8,7 @@ CP=cp
cskernel=Kernel.cs Builtins.cs Cursor.cs JSYNC.cs NieczaCLR.cs
csbackend=CLRBackend.cs
+csxdr=CrossDomainReceiver.cs
# keep this in dependency order
libunits=SAFE CORE JSYNC
@@ -32,7 +33,7 @@ run/Niecza.exe: .fetch-stamp $(patsubst %,src/%.pm6,$(srcunits)) src/niecza
$(RUN_CLR) boot/obj/CLRBackend.exe boot/obj $$nfile.nam $$nfile.dll 0; \
fi; done
$(RUN_CLR) boot/obj/CLRBackend.exe boot/obj MAIN.nam MAIN.exe 1
- $(CP) $(patsubst %,boot/obj/%.dll,Kernel $(libunits) $(srcunits)) run/
+ $(CP) $(patsubst %,boot/obj/%.dll,Kernel CrossDomainReceiver $(libunits) $(srcunits)) run/
$(CP) boot/obj/MAIN.exe run/Niecza.exe
.fetch-stamp: FETCH_URL
@@ -42,12 +43,15 @@ run/Niecza.exe: .fetch-stamp $(patsubst %,src/%.pm6,$(srcunits)) src/niecza
cd boot && unzip niecza.zip
touch .fetch-stamp
-obj/Kernel.dll: $(patsubst %,lib/%,$(cskernel))
- $(CSC) /target:library /out:obj/Kernel.dll /unsafe+ \
- $(patsubst %,lib/%,$(cskernel))
-obj/CLRBackend.exe: $(patsubst %,lib/%,$(csbackend)) obj/Kernel.dll
+obj/CrossDomainReceiver.dll: $(patsubst %,lib/%,$(csxdr))
+ $(CSC) /target:library /out:obj/CrossDomainReceiver.dll \
+ $(patsubst %,lib/%,$(csxdr))
+obj/Kernel.dll: $(patsubst %,lib/%,$(cskernel)) obj/CrossDomainReceiver.dll
+ $(CSC) /target:library /out:obj/Kernel.dll /r:CrossDomainReceiver.dll \
+ /lib:obj /unsafe+ $(patsubst %,lib/%,$(cskernel))
+obj/CLRBackend.exe: $(patsubst %,lib/%,$(csbackend)) obj/Kernel.dll obj/CrossDomainReceiver.dll
$(CSC) /target:exe /lib:obj /out:obj/CLRBackend.exe /r:Kernel.dll \
- $(patsubst %,lib/%,$(csbackend))
+ /r:CrossDomainReceiver.dll $(patsubst %,lib/%,$(csbackend))
test: all
$(RUN_CLR) run/Niecza.exe -c test.pl
View
36 lib/Builtins.cs
@@ -4,6 +4,16 @@
using System.IO;
using System.Reflection;
+namespace Niecza {
+ public class UpCallee: CrossDomainReceiver {
+ public override string[] Call(AppDomain from, string[] args) {
+ return Builtins.UnboxLoS(Kernel.RunInferior(Builtins.upcall_cb.Fetch().Invoke(
+ Kernel.GetInferiorRoot(), new Variable[] { Builtins.BoxLoS(args) },
+ null)));
+ }
+ }
+}
+
public class Builtins {
public static IP6 NominalCheck(string name, DynMetaObject mo, Variable v) {
IP6 r = v.Fetch();
@@ -399,34 +409,28 @@ public class Builtins {
}
private static AppDomain subDomain;
- private static object backend;
+ private static string backend;
// Better, but still fudgy. Relies too mcuh on path structure.
private static AppDomain GetSubDomain() {
if (subDomain != null) return subDomain;
AppDomainSetup ads = new AppDomainSetup();
string obj = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine("..", "obj")));
ads.ApplicationBase = obj;
+ backend = Path.Combine(obj, "CLRBackend.exe");
subDomain = AppDomain.CreateDomain("zyg", null, ads);
- backend = subDomain.CreateInstanceFromAndUnwrap(Path.Combine(obj, "CLRBackend.exe"), "Niecza.CLRBackend.DownCallAcceptor");
return subDomain;
}
+ public static AppDomain up_domain;
+ public static Variable upcall_cb;
+ public static Variable eval_result;
public static Variable DownCall(Variable cb, Variable list) {
GetSubDomain();
- string[] ret = (string[]) backend.GetType().InvokeMember(
- "DownCalled", BindingFlags.Public | BindingFlags.Instance |
- BindingFlags.InvokeMethod, null, backend,
- new object[] { new UpCallee(cb.Fetch()), UnboxLoS(list) });
- return BoxLoS(ret);
- }
- class UpCallee: MarshalByRefObject {
- IP6 func;
- public UpCallee(IP6 func) { this.func = func; }
- public string[] UpCalled(string[] args) {
- return UnboxLoS(Kernel.RunInferior(func.Invoke(
- Kernel.GetInferiorRoot(), new Variable[] { BoxLoS(args) },
- null)));
- }
+ upcall_cb = cb;
+ CrossDomainReceiver r = (CrossDomainReceiver)
+ subDomain.CreateInstanceFromAndUnwrap(backend,
+ "Niecza.CLRBackend.DownCallAcceptor");
+ return BoxLoS(r.Call(AppDomain.CurrentDomain, UnboxLoS(list)));
}
public static Variable ArrayConstructor(Variable bits) {
View
19 lib/CLRBackend.cs
@@ -3978,13 +3978,18 @@ public class CLRBackend {
}
// instantiatable for the sake of reflecty loading
- public class DownCallAcceptor: MarshalByRefObject {
- public string[] DownCalled(object cb, string[] args) {
- if (args[0] == "hello") {
- cb.GetType().InvokeMember(
- "UpCalled", BindingFlags.Public | BindingFlags.Instance |
- BindingFlags.InvokeMethod, null, cb,
- new object[] { new string[] { "world" } });
+ public class DownCallAcceptor: CrossDomainReceiver {
+ public override string[] Call(AppDomain up, string[] args) {
+ Builtins.up_domain = up;
+ if (args[0] == "post_save") {
+ CLRBackend.Main(new string[] { args[1], args[2], args[3], args[4] });
+ return new string[0];
+ } else if (args[0] == "runnam") {
+ Console.WriteLine("directory : {0}", args[1]);
+ Console.WriteLine("unit name : {0}", args[2]);
+ Console.WriteLine("contents : {0}", args[3]);
+ return new string[0];
+ } else if (args[0] == "hello") {
return new string[] { Assembly.GetExecutingAssembly().Location };
} else {
return new string[] { "ERROR" };
View
9 lib/CrossDomainReceiver.cs
@@ -0,0 +1,9 @@
+// This is basically a workaround for ugliness in the CLR APIs for
+// cross-domain communication ...
+
+using System;
+namespace Niecza {
+ public abstract class CrossDomainReceiver : MarshalByRefObject {
+ public abstract string[] Call(AppDomain from, string[] args);
+ }
+}
View
21 src/NieczaBackendDotnet.pm6
@@ -1,13 +1,30 @@
use NieczaBackendNAM;
class NieczaBackendDotnet is NieczaBackendNAM;
+sub upcalled(@strings) {
+ say "upcall: @strings.join('|')";
+ "ERROR";
+}
+
+sub downcall(*@args) {
+ Q:CgOp { (rawscall Builtins,Kernel.DownCall {&upcalled} {@args}) }
+}
+
sub run_subtask($file, *@args) {
Q:CgOp { (rawscall Builtins,Kernel.RunCLRSubtask {$file} {@args}) }
}
+method accept($unitname, $ast is rw, :$main, :$run) {
+ self.save_unit($unitname, $ast);
+ $ast.clear_optrees;
+ $ast = Any;
+ self.post_save($unitname, :$main);
+ $run && self.run($unitname);
+}
+
method post_save($name, :$main) {
my $fname = $name.split('::').join('.');
- run_subtask($.obj_dir.IO.append("CLRBackend.exe"),
+ downcall("post_save",
$.obj_dir, $fname ~ ".nam", $fname ~ ($main ?? ".exe" !! ".dll"),
$main ?? "1" !! "0");
}
@@ -16,3 +33,5 @@ method run($name) {
my $fname = $name.split('::').join('.');
run_subtask($.obj_dir.IO.append($fname ~ ".exe"), @$.run_args);
}
+
+
View
8 src/NieczaBackendNAM.pm6
@@ -33,3 +33,11 @@ method post_save($name, :$main) { #OK not used
method run($name) { #OK not used
die "nam backend does not support running code";
}
+
+method accept($unitname, $ast is rw, :$main, :$run) {
+ self.save_unit($unitname, $ast);
+ $ast.clear_optrees;
+ $ast = Any;
+ self.post_save($unitname, :$main);
+ $run && self.run($unitname);
+}
View
8 src/NieczaCompiler.pm6
@@ -24,13 +24,7 @@ method !compile($unitname, $filename, $modtime, $source, $main, $run, $end) {
$.frontend.typename => { $ast = $.frontend.parse(:$unitname,
:$filename, :$modtime, :$source); },
(map -> $st { $st.typename => { $ast = $st.invoke($ast) } }, @$.stages),
- ($.backend.typename ~ "-save") =>
- { $.backend.save_unit($unitname, $ast);
- $ast.clear_optrees; $ast = Any },
- ($.backend.typename ~ "-post") =>
- { $.backend.post_save($unitname, :$main); },
- ($.backend.typename ~ "-run") =>
- { $run && $.backend.run($unitname); },
+ $.backend.typename => { $.backend.accept($unitname, $ast, :$main, :$run) },
);
for @steps -> $step {

0 comments on commit b94a627

Please sign in to comment.
Something went wrong with that request. Please try again.