Beispiel #1
0
 /// <summary>
 /// 特定の<see cref="KecaknoahScriptClassInfo"/>を元にして、インスタンスを生成します。
 /// コンストラクタがあった場合、呼び出します。
 /// </summary>
 /// <param name="klass">クラス</param>
 /// <param name="ctx">コンテキスト</param>
 /// <param name="ctorArgs">コンストラクタ引数</param>
 public KecaknoahInstance(KecaknoahScriptClassInfo klass, KecaknoahContext ctx, KecaknoahObject[] ctorArgs)
 {
     Class = klass;
     ExtraType = klass.Name;
     LocalFieldReferences = localReferences;
     InstanceMethodReferences = methodReferences;
     foreach (var i in klass.LocalInfos)
     {
         localReferences[i.Name] = new KecaknoahReference() { IsLeftValue = true };
         if (i.InitializeIL != null)
         {
             localReferences[i.Name].RawObject = ctx.ExecuteExpressionIL(i.InitializeIL);
         }
     }
     foreach (var i in klass.methods)
         methodReferences[i.Name] = new KecaknoahReference()
         {
             IsLeftValue = true,
             RawObject = new KecaknoahScriptFunction(this, i)
         };
     var ctor = klass.classMethods.FirstOrDefault(p => p.Name == "new");
     if (ctor != null)
     {
         new KecaknoahScriptFunction(this, ctor).Call(ctx, ctorArgs);
     }
 }
 private KecaknoahFunctionResult InstanceCreareInstance(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     List<object> ia = new List<object>();
     foreach (var i in args)
     {
         switch (i.Type)
         {
             case TypeCode.Boolean:
                 ia.Add(i.ToBoolean());
                 break;
             case TypeCode.Double:
                 ia.Add(i.ToDouble());
                 break;
             case TypeCode.Int64:
                 ia.Add(i.ToInt32());
                 break;
             case TypeCode.String:
                 ia.Add(i.ToString());
                 break;
             case TypeCode.Empty:
                 ia.Add(null);
                 break;
             default:
                 var t = i as KecaknoahDynamicLibraryObject;
                 ia.Add(t != null ? t.rawobj : null);
                 break;
         }
     }
     var obj = Activator.CreateInstance(type, ia.ToArray());
     return new KecaknoahDynamicLibraryObject(obj).NoResume();
 }
 /// <summary>
 /// 呼び出します。
 /// </summary>
 /// <param name="context"></param>
 /// <param name="args">引数</param>
 /// <returns>返り値</returns>
 protected internal override KecaknoahFunctionResult Call(KecaknoahContext context, KecaknoahObject[] args)
 {
     var sf = new KecaknoahStackFrame(context, BaseMethod.Codes);
     sf.Locals["self"] = KecaknoahReference.CreateRightReference(Instance);
     sf.Arguments = args;
     var r = sf.Resume();
     return new KecaknoahFunctionResult(sf.ReturningObject, r);
 }
 private static KecaknoahFunctionResult ClassLoadFile(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     var name = Path.GetFullPath(args[0].ToString());
     var catalog = new DirectoryCatalog(Path.GetDirectoryName(name), Path.GetFileName(name));
     var result = new KecaknoahExtensionLibrary();
     var container = new CompositionContainer(catalog);
     container.ComposeParts(result);
     return result.NoResume();
 }
 /// <summary>
 /// 呼び出します。
 /// </summary>
 /// <param name="context"></param>
 /// <param name="args">引数</param>
 /// <returns>返り値</returns>
 protected internal override KecaknoahFunctionResult Call(KecaknoahContext context, KecaknoahObject[] args)
 {
     if (args != null)
     {
         CurrentFrame = new KecaknoahStackFrame(context, BaseMethod.Codes);
         CurrentFrame.Locals["self"] = KecaknoahReference.Right(Instance);
         CurrentFrame.Arguments = args;
     }
     var r = CurrentFrame.Resume();
     return new KecaknoahFunctionResult(CurrentFrame.ReturningObject, r);
 }
Beispiel #6
0
 private KecaknoahFunctionResult InstanceGetInteger(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     switch (args.Length)
     {
         case 0:
             return random.Next().AsKecaknoahInteger().NoResume();
         case 1:
             var a = args.ExpectInt32(1, false);
             return random.Next(a[0]).AsKecaknoahInteger().NoResume();
         default:
             var a2 = args.ExpectInt32(2, false);
             return random.Next(a2[0], a2[1]).AsKecaknoahInteger().NoResume();
     }
 }
Beispiel #7
0
 private static KecaknoahFunctionResult ToInteger(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     switch (args[0].Type)
     {
         case TypeCode.Boolean:
             return Convert.ToInt64(args[0].ToBoolean()).AsKecaknoahInteger().NoResume();
         case TypeCode.Int64:
             return args[0].NoResume();
         case TypeCode.Double:
             return Convert.ToInt64(args[0].ToDouble()).AsKecaknoahInteger().NoResume();
         case TypeCode.String:
             return Convert.ToInt64(args[0].ToString()).AsKecaknoahInteger().NoResume();
         default:
             return 0.AsKecaknoahInteger().NoResume();
     }
 }
Beispiel #8
0
 /// <summary>
 /// 特定の<see cref="KecaknoahScriptClassInfo"/>を元にして、インスタンスを生成します。
 /// コンストラクタがあった場合、呼び出します。
 /// </summary>
 /// <param name="klass">クラス</param>
 /// <param name="ctx">コンテキスト</param>
 /// <param name="ctorArgs">コンストラクタ引数</param>
 public KecaknoahInstance(KecaknoahScriptClassInfo klass, KecaknoahContext ctx, KecaknoahObject[] ctorArgs)
 {
     Class = klass;
     LocalFieldReferences = localReferences;
     InstanceMethodReferences = methodReferences;
     foreach (var i in klass.Locals) localReferences[i] = new KecaknoahReference() { IsLeftValue = true };
     foreach (var i in klass.methods)
         methodReferences[i.Name] = new KecaknoahReference()
         {
             IsLeftValue = true,
             RawObject = new KecaknoahScriptFunction(this, i)
         };
     if (klass.classMethods.Any(p => p.Name == "new"))
     {
         var ctor = klass.classMethods.First(p => p.Name == "new");
         new KecaknoahScriptFunction(this, ctor).Call(ctx, ctorArgs);
     }
 }
Beispiel #9
0
 /// <summary>
 /// 特定の<see cref="KecaknoahInteropClassInfo"/>を元にして、インスタンスを生成します。
 /// コンストラクタがあった場合、呼び出します。
 /// </summary>
 /// <param name="klass">クラス</param>
 /// <param name="ctx">コンテキスト</param>
 /// <param name="ctorArgs">コンストラクタ引数</param>
 public KecaknoahInstance(KecaknoahInteropClassInfo klass, KecaknoahContext ctx, KecaknoahObject[] ctorArgs)
 {
     Class = klass;
     ExtraType = klass.Name;
     LocalFieldReferences = localReferences;
     InstanceMethodReferences = methodReferences;
     foreach (var i in klass.LocalInfos)
     {
         localReferences[i.Name] = KecaknoahReference.Left(i.Value.AsByValValue());
     }
     foreach (var i in klass.methods)
         methodReferences[i.Name] = new KecaknoahReference()
         {
             IsLeftValue = true,
             RawObject = new KecaknoahInteropFunction(this, i.Body)
         };
     var ctor = klass.classMethods.FirstOrDefault(p => p.Name == "new");
     if (ctor != null) ctor.Body(ctx, this, ctorArgs);
 }
Beispiel #10
0
 private KecaknoahFunctionResult InstanceMatch(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     Match result;
     switch (args.Length)
     {
         case 1:
             result = regex.Match(args[0].ToString());
             break;
         case 2:
             result = regex.Match(args[0].ToString(), args[1].ToInt32());
             break;
         case 3:
             result = regex.Match(args[0].ToString(), args[1].ToInt32(), args[2].ToInt32());
             break;
         default:
             result = null;
             break;
     }
     return new KecaknoahMatch(result).NoResume();
 }
Beispiel #11
0
 private KecaknoahFunctionResult InstanceReduce(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     var r = list.Select(p => p.RawObject).Aggregate((p, q) => args[0].Call(ctx, new[] { p, q }).ReturningObject);
     return r.NoResume();
 }
Beispiel #12
0
 private KecaknoahFunctionResult InstanceMap(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     var r = list.Select(p => args[0].Call(ctx, new[] { p.RawObject }).ReturningObject);
     return new KecaknoahList(r).NoResume();
 }
Beispiel #13
0
 private KecaknoahFunctionResult InstanceToLower(KecaknoahContext context, KecaknoahObject self, KecaknoahObject[] args) => raw.ToLower().AsKecaknoahString().NoResume();
Beispiel #14
0
 private KecaknoahFunctionResult InstanceEndsWith(KecaknoahContext context, KecaknoahObject self, KecaknoahObject[] args) => raw.EndsWith(args[0].ToString()).AsKecaknoahBoolean().NoResume();
Beispiel #15
0
 private KecaknoahFunctionResult InstanceReplace(KecaknoahContext context, KecaknoahObject self, KecaknoahObject[] args) => raw.Replace(args[0].ToString(), args[1].ToString()).AsKecaknoahString().NoResume();
Beispiel #16
0
 private static KecaknoahFunctionResult ClassCosh(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     var result = Math.Cosh(args[0].ToDouble());
     return result.AsKecaknoahFloat().NoResume();
 }
Beispiel #17
0
 private static KecaknoahFunctionResult ClassPow(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     var da = args.ExpectDouble(2, false);
     var result = Math.Pow(da[0], da[1]);
     return result.AsKecaknoahFloat().NoResume();
 }
Beispiel #18
0
#pragma warning restore 1591

        #endregion

        #region クラスメソッド
        /* 
        当たり前ですがクラスメソッド呼び出しではselfはnullになります。
        selfに代入するのではなく生成したのをNoResumeで返却します。
        */

        private static KecaknoahFunctionResult ClassNew(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args) => new KecaknoahList().NoResume();
Beispiel #19
0
 private static KecaknoahFunctionResult ClassLimit(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     var da = args.ExpectDouble(3, false);
     var result = da[0];
     if (result < da[1]) result = da[1];
     if (result > da[2]) result = da[2];
     return result.AsKecaknoahFloat().NoResume();
 }
Beispiel #20
0
 private KecaknoahFunctionResult InstanceClear(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     list.Clear();
     return KecaknoahNil.Instance.NoResume();
 }
Beispiel #21
0
 private static KecaknoahFunctionResult ClassSign(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     var result = Math.Sign(args[0].ToDouble());
     return result.AsKecaknoahInteger().NoResume();
 }
Beispiel #22
0
 private KecaknoahFunctionResult InstanceInsert(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     list.Insert((int)args[0].ToInt64(), new KecaknoahReference { IsLeftValue = true, RawObject = args[1] });
     return KecaknoahNil.Instance.NoResume();
 }
Beispiel #23
0
 private KecaknoahFunctionResult InstanceSubstring(KecaknoahContext context, KecaknoahObject self, KecaknoahObject[] args)
 {
     var t = self.ToString();
     switch (args.Length)
     {
         case 0:
             return "".AsKecaknoahString().NoResume();
         case 1:
             return t.Substring((int)args[0].ToInt64()).AsKecaknoahString().NoResume();
         default:
             return t.Substring((int)args[0].ToInt64(), (int)args[1].ToInt64()).AsKecaknoahString().NoResume();
     }
 }
Beispiel #24
0
 private KecaknoahFunctionResult InstanceRemoveAt(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     list.RemoveAt((int)args[0].ToInt64());
     return KecaknoahNil.Instance.NoResume();
 }
Beispiel #25
0
 private KecaknoahFunctionResult InstanceSplit(KecaknoahContext context, KecaknoahObject self, KecaknoahObject[] args)
 {
     var l = args[0];
     var op = StringSplitOptions.None;
     if (args.Length >= 2)
     {
         var flag = args[1].ToBoolean();
         if (flag) op = StringSplitOptions.RemoveEmptyEntries;
     }
     if (l.ExtraType == "Array")
     {
         var list = l.ToStringArray();
         var result = raw.Split(list.ToArray(), op);
         return new KecaknoahArray(result.Select(p => p.AsKecaknoahString())).NoResume();
     }
     else
     {
         var str = l.ToString();
         var result = raw.Split(new[] { str }, op);
         return new KecaknoahArray(result.Select(p => p.AsKecaknoahString())).NoResume();
     }
 }
Beispiel #26
0
 private KecaknoahFunctionResult InstanceRemoveBy(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     list.RemoveAll(p => args[0].CallAsPredicate(ctx, p.RawObject));
     return KecaknoahNil.Instance.NoResume();
 }
Beispiel #27
0
 private KecaknoahFunctionResult InstancePadRight(KecaknoahContext context, KecaknoahObject self, KecaknoahObject[] args) => raw.PadRight(args[0].ToInt32()).AsKecaknoahString().NoResume();
Beispiel #28
0
 private KecaknoahFunctionResult InstanceEach(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     foreach (var i in list) args[0].Call(ctx, new[] { i.RawObject });
     return KecaknoahNil.Instance.NoResume();
 }
Beispiel #29
0
 private static KecaknoahFunctionResult ClassJoin(KecaknoahContext context, KecaknoahObject self, KecaknoahObject[] args)
 {
     var ls = args[1].ToStringArray();
     var s = args[0].ToString();
     var result = string.Join(s, ls);
     return result.AsKecaknoahString().NoResume();
 }
Beispiel #30
0
 private KecaknoahFunctionResult InstanceFilter(KecaknoahContext ctx, KecaknoahObject self, KecaknoahObject[] args)
 {
     var r = list.Where(p => args[0].CallAsPredicate(ctx, p.RawObject));
     return new KecaknoahList(r).NoResume();
 }