Example #1
0
  /// <summary>
  /// Handles the various bits of initialization that are needed.
  /// Probably needs some don't-dupe-this work.
  /// </summary>
  public static ThreadContext Initialize(String settingName) {
    // Bootstrap the meta-model.
    RegisterRepresentations();
    RakudoObject knowHOW = KnowHOWBootstrapper.Bootstrap();
    RakudoObject knowHOWAttribute = KnowHOWBootstrapper.SetupKnowHOWAttribute(knowHOW);

    // Either load a named setting or use the fake bootstrapping one.
    Context settingContext =
        // Comment out the next line to always use the fake Setting.
        (settingName != null)
            ? LoadSetting(settingName, knowHOW, knowHOWAttribute)
            : BootstrapSetting(knowHOW, knowHOWAttribute);

    // Cache native capture and LLCode type object.
    CaptureHelper.CaptureTypeObject = settingContext.LexPad.GetByName("capture");
    CodeObjectUtility.LLCodeTypeObject =
        (RakudoCodeRef.Instance) settingContext.LexPad.GetByName("NQPCode");

    // Create an execution domain and a thread context for it.
    ExecutionDomain executionDomain = new ExecutionDomain();
    executionDomain.Setting = settingContext;
    ThreadContext threadContext = new ThreadContext();
    threadContext.Domain = executionDomain;
    threadContext.CurrentContext = settingContext;
    threadContext.DefaultBoolBoxType = settingContext.LexPad.GetByName("NQPInt");
    threadContext.DefaultIntBoxType = settingContext.LexPad.GetByName("NQPInt");
    threadContext.DefaultNumBoxType = settingContext.LexPad.GetByName("NQPNum");
    threadContext.DefaultStrBoxType = settingContext.LexPad.GetByName("NQPStr");
    threadContext.DefaultListType = settingContext.LexPad.GetByName("NQPList");
    threadContext.DefaultArrayType = settingContext.LexPad.GetByName("NQPArray");
    threadContext.DefaultHashType = settingContext.LexPad.GetByName("NQPHash");

    return threadContext;
  }
Example #2
0
  /// <summary>
  /// Loads the setting with the given name.
  /// </summary>
  /// <param name="Name"></param>
  /// <param name="KnowHOW"></param>
  /// <returns></returns>
  public static Context LoadSetting(
      String settingName, RakudoObject knowHOW, RakudoObject knowHOWAttribute) {
    // Load the assembly.
    // This is quite unlike the C# version
    // System.err.println("Init.LoadSetting begin loading " + settingName );
    ClassLoader loader = ClassLoader.getSystemClassLoader();
    Class<?> classNQPSetting = null; // grrr, a wildcard type :-(
    try {
      classNQPSetting = loader.loadClass(settingName);
    } catch (ClassNotFoundException ex) {
      System.err.println("Class " + settingName + " not found: " + ex.getMessage());
      ex.printStackTrace();
      System.exit(1);
    } catch (Exception ex) {
      System.err.println("loadClass(\"" + settingName + "\") exception: " + ex.getMessage());
      System.exit(1);
    }
    assert classNQPSetting != null : "classNQPSetting is null";

    // Find the setting type and its LoadSetting method.
    java.lang.reflect.Method methodLoadSetting = null;
    try {
      methodLoadSetting = classNQPSetting.getMethod("LoadSetting");
    } catch (NoSuchMethodException ex) {
      System.err.println("Method LoadSetting not found: " + ex.getMessage());
      System.exit(1);
    } catch (Exception ex) {
      System.err.println("getMethod(\"LoadSetting\") exception: " + ex.getMessage());
      ex.printStackTrace();
      System.exit(1);
    }

    assert methodLoadSetting != null : "methodLoadSetting is null";

    // Run it to get the context we want.
    Context settingContext = null;
    try {
      settingContext = (Context) methodLoadSetting.invoke(null);
    } catch (IllegalAccessException ex) {
      System.err.println("Illegal access: " + ex.getMessage());
      System.exit(1);
    } catch (java.lang.reflect.InvocationTargetException ex) {
      System.err.println("Invocation target exception: " + ex.getMessage());
      ex.printStackTrace();
      System.exit(1);
    }

    // Fudge a few more things in.
    // XXX Should be able to toss all of these but KnowHOW.
    settingContext.LexPad.Extend(
        new String[] {"KnowHOW", "KnowHOWAttribute", "print", "say", "capture"});
    settingContext.LexPad.SetByName("KnowHOW", knowHOW);
    settingContext.LexPad.SetByName("KnowHOWAttribute", knowHOWAttribute);
    settingContext.LexPad.SetByName(
        "print",
        CodeObjectUtility.WrapNativeMethod(
            new RakudoCodeRef.IFunc_Body() { // an anonymous class where C# has a => (lambda)
              public RakudoObject Invoke(
                  ThreadContext tc, RakudoObject self, RakudoObject capture) {
                int numPositionals = CaptureHelper.NumPositionals(capture);
                for (int i = 0; i < numPositionals; i++) {
                  RakudoObject value = CaptureHelper.GetPositional(capture, i);
                  RakudoObject strMeth =
                      self.getSTable().FindMethod.FindMethod(tc, value, "Str", 0);
                  RakudoObject strVal =
                      strMeth
                          .getSTable()
                          .Invoke
                          .Invoke(tc, strMeth, CaptureHelper.FormWith(new RakudoObject[] {value}));
                  System.out.print(Ops.unbox_str(null, strVal));
                }
                return CaptureHelper.Nil();
              }
            }));
    settingContext.LexPad.SetByName(
        "say",
        CodeObjectUtility.WrapNativeMethod(
            new RakudoCodeRef.IFunc_Body() { // an anonymous class where C# has a => (lambda)
              public RakudoObject Invoke(
                  ThreadContext tc, RakudoObject self, RakudoObject capture) {
                int numPositionals = CaptureHelper.NumPositionals(capture);
                for (int i = 0; i < numPositionals; i++) {
                  RakudoObject value = CaptureHelper.GetPositional(capture, i);
                  RakudoObject strMeth =
                      self.getSTable().FindMethod.FindMethod(tc, value, "Str", 0);
                  RakudoObject strVal =
                      strMeth
                          .getSTable()
                          .Invoke
                          .Invoke(tc, strMeth, CaptureHelper.FormWith(new RakudoObject[] {value}));
                  System.out.print(Ops.unbox_str(null, strVal));
                }
                System.out.println();
                return CaptureHelper.Nil();
              }
            }));
    settingContext.LexPad.SetByName(
        "capture", REPRRegistry.get_REPR_by_name("P6capture").type_object_for(null, null));

    return settingContext;
  }