Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow different compartments to have different load paths and compile…
…r hooks
  • Loading branch information
sorear committed Nov 19, 2012
1 parent ee5dcf1 commit da13155
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 42 deletions.
18 changes: 8 additions & 10 deletions lib/Builtins.cs
Expand Up @@ -1971,19 +1971,18 @@ public partial class Builtins {
System.Diagnostics.Process.Start(file, args).WaitForExit();
}

[TrueGlobal] internal static System.Collections.IDictionary upcall_receiver;
internal static object UpCall(object[] args) {
return upcall_receiver[args];
internal static object UpCall(Compartment c, params object[] args) {
return c.upcall_receiver[args];
}
public static Frame simple_eval(Frame th, Variable str) {
if (upcall_receiver == null)
if (th.info.setting.upcall_receiver == null)
return Kernel.Die(th, "Cannot eval; no compiler available");
SubInfo outer = th.caller.info;
object r = UpCall(new object[] { "eval",
object r = UpCall(th.info.setting, "eval",
str.Fetch().mo.mro_raw_Str.Get(str),
new Niecza.CLRBackend.Handle(outer),
new Niecza.CLRBackend.Handle(th.caller)
});
);
if (r is Exception)
return Kernel.Die(th, ((Exception)r).Message);
P6any sub = Kernel.MakeSub(((RuntimeUnit)Niecza.CLRBackend.Handle.Unbox(r)).mainline, th.caller);
Expand All @@ -1997,12 +1996,11 @@ public partial class Builtins {
// TODO: it would be better if the compiler could be modified to
// compile a regex directly as the mainline
if (!th.info.rx_compile_cache.TryGetValue(code, out main)) {
if (upcall_receiver == null)
if (th.info.setting.upcall_receiver == null)
throw new NieczaException("Cannot eval; no compiler available");
object r = UpCall(new object[] { "eval",
object r = UpCall(th.info.setting, "eval",
"regex {" + code + "}",
new Niecza.CLRBackend.Handle(th.info)
});
new Niecza.CLRBackend.Handle(th.info));
if (r is Exception)
throw new NieczaException(((Exception)r).Message);
main = ((RuntimeUnit)Niecza.CLRBackend.Handle.Unbox(r)).mainline;
Expand Down
11 changes: 5 additions & 6 deletions lib/CodeGen.cs
Expand Up @@ -3418,8 +3418,6 @@ class NamProcessor {
}

public class Backend {
[TrueGlobal]
public static string obj_dir = AppDomain.CurrentDomain.BaseDirectory;
[TrueGlobal]
public static string prefix = (typeof(Backend).Assembly.GetName().Name == "Kernel") ? "" : "Run.";
[TrueGlobal]
Expand Down Expand Up @@ -3646,13 +3644,14 @@ public class DowncallReceiver : CallReceiver {
Kernel.TraceFlags = Kernel.TRACE_CUR;
Kernel.TraceCount = Kernel.TraceFreq = 1;
}
Kernel.InitGlobal();
Backend.obj_dir = (string)args[1];
Builtins.upcall_receiver = (System.Collections.IDictionary)args[2];
var c = (Compartment)Handle.Unbox(args[1]);
c.obj_dir = (string)args[2];
c.upcall_receiver = (System.Collections.IDictionary)args[3];
return null;
}
public static object push_compartment(object[] args) {
var nc = new Compartment();
Kernel.InitGlobal();
Kernel.InitCompartment(nc);
return Handle.Wrap(nc);
}
Expand Down Expand Up @@ -3711,7 +3710,7 @@ public class DowncallReceiver : CallReceiver {
if (Config.SerFailInfo)
Console.WriteLine("Thaw {0} failed: >>>{1}<<<", oname, ex);
// assume stale at first
object r1 = Builtins.UpCall(new object[] {
object r1 = Builtins.UpCall(ru.setting, new object[] {
"compile_unit", oname });
if (r1 != null)
return r1;
Expand Down
21 changes: 13 additions & 8 deletions lib/Kernel.cs
Expand Up @@ -434,7 +434,7 @@ sealed class EmitUnit {
new AssemblyName(asm_name),
(dll_name == null ? AssemblyBuilderAccess.Run :
AssemblyBuilderAccess.RunAndSave),
Backend.obj_dir);
s.obj_dir);

mod_builder = dll_name == null ?
asm_builder.DefineDynamicModule(asm_name) :
Expand Down Expand Up @@ -995,11 +995,11 @@ public sealed class RuntimeUnit : IFreeze {

n.name = tb.String();
string[] srcinfo = tb.Strings();
if (Builtins.upcall_receiver != null) {
if (tb.setting.upcall_receiver != null) {
object[] args = new object[srcinfo.Length + 1];
Array.Copy(srcinfo, 0, args, 1, srcinfo.Length);
args[0] = "check_dated";
object result = Builtins.UpCall(args);
object result = Builtins.UpCall(tb.setting, args);
if (result is Exception)
throw (Exception)result;
if ((string)result != "ok")
Expand All @@ -1026,7 +1026,7 @@ public sealed class RuntimeUnit : IFreeze {
tb.ObjRef();
}
} else {
Assembly assembly = Assembly.Load(n.asm_name);
Assembly assembly = Assembly.LoadFrom(Path.Combine(tb.setting.obj_dir, n.dll_name));
n.type = tb.type = assembly.GetType(n.asm_name, true);
n.constTable = (Constants)Activator.CreateInstance(n.type);
n.constTable.setting = tb.setting;
Expand Down Expand Up @@ -4569,6 +4569,8 @@ public class Compartment {
[CORESaved] public BoxObject<int> FalseV;

internal ObjectRegistry reg;
public string obj_dir = AppDomain.CurrentDomain.BaseDirectory;
internal System.Collections.IDictionary upcall_receiver;

// SubInfo objects can be mutated at runtime by .wrap so they
// must be containerized
Expand Down Expand Up @@ -4993,7 +4995,10 @@ public class Kernel {
return nw;
}

internal static bool gl_inited = false; // TODO threads?
internal static void InitGlobal() {
if (gl_inited) return;
gl_inited = true;
Random r = new Random();
VarHash.string_hash_argument =
(uint)r.Next(VarHash.HASH_ARG_MAX + 1);
Expand All @@ -5003,6 +5008,8 @@ public class Kernel {
}

internal static void InitCompartment(Compartment c) {
InitGlobal();

c.reg = new ObjectRegistry(c);

c.AutoThreadSubSI = new SubInfo(c, "KERNEL AutoThreadSub",
Expand Down Expand Up @@ -6677,7 +6684,6 @@ class LastFrameNode {
public static void MainHandler(string uname, string[] args) {
var c = new Compartment();
InitCompartment(c);
InitGlobal();
commandArgs = args;

RuntimeUnit ru = (RuntimeUnit)
Expand Down Expand Up @@ -6715,7 +6721,6 @@ class LastFrameNode {
var comp = new Compartment();

InitCompartment(comp);
InitGlobal();

if (cmd == "-field-inventory") {
foreach (Type ty in typeof(Kernel).Assembly.GetTypes()) {
Expand Down Expand Up @@ -6771,15 +6776,15 @@ class LastFrameNode {

// allow loading of Run. files, but don't try to load the
// assemblies, since we're not Run.Kernel
Backend.obj_dir = fromdir;
comp.obj_dir = fromdir;
Backend.prefix = "Run.";
Backend.cross_level_load = true;

RuntimeUnit root = (RuntimeUnit)
comp.reg.LoadUnit("MAIN").root;

// reset for writing
Backend.obj_dir = AppDomain.CurrentDomain.BaseDirectory;
comp.obj_dir = AppDomain.CurrentDomain.BaseDirectory;
Backend.prefix = "";

RewriteUnits(root, root, new HashSet<RuntimeUnit>());
Expand Down
4 changes: 2 additions & 2 deletions lib/Serialize.cs
Expand Up @@ -151,7 +151,7 @@ struct ObjRef {
return units[name];
}

string file = Path.Combine(Backend.obj_dir, Backend.prefix +
string file = Path.Combine(setting.obj_dir, Backend.prefix +
name.Replace("::",".") + ".ser");
byte[] bytes = File.ReadAllBytes(file);

Expand Down Expand Up @@ -203,7 +203,7 @@ struct ObjRef {
throw new InvalidOperationException("unit " +name+ " exists");

bool success = false;
string file = Path.Combine(Backend.obj_dir, Backend.prefix +
string file = Path.Combine(setting.obj_dir, Backend.prefix +
name.Replace("::",".") + ".ser");

FreezeBuffer fb = new FreezeBuffer(this, su);
Expand Down
20 changes: 5 additions & 15 deletions src/CompilerBlob.cs
Expand Up @@ -53,17 +53,6 @@ public class Downcaller {
static STable StrMO, NumMO, ListMO, AnyMO, BoolMO;
static string obj_dir;

// let the CLR load assemblies from obj/ too
static Assembly ObjLoader(object source, ResolveEventArgs e) {
string name = e.Name;
if (name.IndexOf(',') >= 0)
name = name.Substring(0, name.IndexOf(','));
string file = Path.Combine(obj_dir, name + ".dll");
if (File.Exists(file))
return Assembly.LoadFrom(file);
else
return null;
}
// Better, but still fudgy. Relies too much on path structure.
public static void InitSlave(Variable cb, P6any cmd_obj_dir, Variable unit,
Variable staticSub, Variable type, Variable param, Variable value,
Expand Down Expand Up @@ -98,12 +87,13 @@ public class Downcaller {
);
}

AppDomain.CurrentDomain.AssemblyResolve += ObjLoader;
var down_asm = Assembly.LoadFrom(Path.Combine(obj_dir, "Run.Kernel.dll"));

upcall_cb = cb;
responder = (IDictionary) Activator.CreateInstance(Type.GetType(
"Niecza.CLRBackend.DowncallReceiver,Run.Kernel", true));
RawDowncall("set_binding", obj_dir, new UpcallReceiver());
responder = (IDictionary) down_asm.CreateInstance("Niecza.CLRBackend.DowncallReceiver");
}
public static void InitCompartment(Variable c) {
RawDowncall("set_binding", DCArg(c), obj_dir, new UpcallReceiver());
}
public static object RawDowncall(params object[] args) {
return responder[args];
Expand Down
6 changes: 5 additions & 1 deletion src/NieczaBackendDotnet.pm6
Expand Up @@ -341,7 +341,11 @@ class Unit {
}
}

method push_compartment() { downcall("push_compartment") }
method push_compartment() {
my \c = downcall("push_compartment");
Q:CgOp { (rnull (rawscall Niecza.Downcaller,CompilerBlob.InitCompartment {c})) };
c;
}
method pop_compartment($c) { downcall("pop_compartment", $c) }
method get_codepoint($str) { downcall("get_codepoint", $str) }
Expand Down

0 comments on commit da13155

Please sign in to comment.