/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> public static string VariantToExpressionString(Variant val) { // convert given variant to string which can be interpret as an expression. // this function does not convert objects ( returns empty string ) if (val.IsVoid()) { return "void"; } else { if (val.IsInteger()) { return val.AsString(); } else { if (val.IsReal()) { string s = Variant.RealToHexString(val.AsDouble()); return s + " /* " + val.AsString() + " */"; } else { if (val.IsString()) { string s = LexBase.EscapeC(val.AsString()); return "\"" + s + "\""; } else { if (val.IsOctet()) { string s = Variant.OctetToListString(val.AsOctet()); return "<%" + s + "%>"; } else { return string.Empty; } } } } } }
/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> /// <exception cref="Kirikiri.Tjs2.TJSException"></exception> public static void SaveStructuredDataForObject(Dispatch2 dsp, AList<Dispatch2> stack , TextWriteStreamInterface stream, string indentstr) { // check object recursion int count = stack.Count; for (int i = 0; i < count; i++) { Dispatch2 d = stack[i]; if (d == dsp) { // object recursion detected stream.Write("null /* object recursion detected */"); return; } } // determin dsp's object type DictionaryNI dicni; ArrayNI arrayni; if (dsp != null) { dicni = (DictionaryNI)dsp.GetNativeInstance(DictionaryClass.ClassID); if (dicni != null) { // dictionary stack.AddItem(dsp); dicni.SaveStructuredData(stack, stream, indentstr); stack.Remove(stack.Count - 1); return; } else { arrayni = (ArrayNI)dsp.GetNativeInstance(ArrayClass.ClassID); if (arrayni != null) { // array stack.AddItem(dsp); arrayni.SaveStructuredData(stack, stream, indentstr); stack.Remove(stack.Count - 1); return; } else { // other objects stream.Write("null /* (object) \""); // stored as a null Variant val = new Variant(dsp, dsp); stream.Write(LexBase.EscapeC(val.AsString())); stream.Write("\" */"); return; } } } stream.Write("null"); }
/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> public static void CharacterCodeOf(Variant val) { string str = val.AsString(); if (str != null) { int v = str[0]; val.Set(v); } else { val.Set(0); } }
/// <exception cref="Kirikiri.Tjs2.TJSException"></exception> /// <exception cref="Kirikiri.Tjs2.VariantException"></exception> private static void GetStringProperty(Variant result, Variant str, string name) { // processes properties toward strings. if (name == null) { ThrowFrom_tjs_error(Error.E_MEMBERNOTFOUND, string.Empty); } if (name.Equals("length")) { // get string length string s = str.AsString(); if (s == null) { result.Set(0); } else { // tTJSVariantString::GetLength can return zero if 'this' is NULL result.Set(s.Length); } return; } else { if (name[0] >= '0' && name[0] <= '9') { string s = str.AsString(); int n = int.Parse(name); int len = s.Length; if (n == len) { result.Set(string.Empty); return; } if (n < 0 || n > len) { throw new TJSException(Error.RangeError); } result.Set(Sharpen.Runtime.Substring(s, n, n + 1)); return; } } ThrowFrom_tjs_error(Error.E_MEMBERNOTFOUND, name); }
/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> /// <exception cref="Kirikiri.Tjs2.TJSException"></exception> /// <exception cref="Kirikiri.Tjs2.CompileException"></exception> protected internal virtual void Eval(Variant val, Dispatch2 objthis, bool resneed ) { Variant res = new Variant(); string str = val.AsString(); if (str.Length > 0) { if (resneed) { GetOwner().EvalExpression(str, res, objthis, null, 0); } else { GetOwner().EvalExpression(str, null, objthis, null, 0); } } if (resneed) { val.Set(res); } }
/// <exception cref="Kirikiri.Tjs2.TJSException"></exception> /// <exception cref="Kirikiri.Tjs2.VariantException"></exception> protected internal static void TypeOfMemberIndirect(Variant result, Variant target , Variant member, Dispatch2 objthis, int flags) { if (target.IsObject()) { VariantClosure clo = target.AsObjectClosure(); if (member.IsInteger() != true) { string str = member.AsString(); int hr = clo.PropGet(flags, str, result, clo.mObjThis != null ? clo.mObjThis : objthis ); if (hr == Error.S_OK) { TypeOf(result); } else { if (hr == Error.E_MEMBERNOTFOUND) { result.Set("undefined"); } else { if (hr < 0) { ThrowFrom_tjs_error(hr, str); } } } } else { int hr = clo.PropGetByNum(flags, member.AsInteger(), result, clo.mObjThis != null ? clo.mObjThis : objthis); if (hr == Error.S_OK) { TypeOf(result); } else { if (hr == Error.E_MEMBERNOTFOUND) { result.Set("undefined"); } else { if (hr < 0) { ThrowFrom_tjs_error_num(hr, member.AsInteger()); } } } } } else { if (target.IsString()) { GetStringProperty(result, target, member); TypeOf(result); } else { if (target.IsOctet()) { GetOctetProperty(result, target, member); TypeOf(result); } else { string mes = Error.VariantConvertErrorToObject.Replace("%1", Utils.VariantToReadableString (target)); throw new VariantException(mes); } } } }
/// <exception cref="Kirikiri.Tjs2.TJSException"></exception> /// <exception cref="Kirikiri.Tjs2.VariantException"></exception> protected internal static void ThrowScriptException(Variant val, ScriptBlock block , int srcpos) { string msg = null; if (val.IsObject()) { VariantClosure clo = val.AsObjectClosure(); if (clo.mObject != null) { Variant v2 = new Variant(); string message_name = "message"; int hr = clo.PropGet(0, message_name, v2, null); if (hr >= 0) { msg = "script exception : " + v2.AsString(); } } } if (msg == null || msg.Length == 0) { msg = "script exception"; } throw new TJSScriptException(msg, block, srcpos, val); }
/// <exception cref="Kirikiri.Tjs2.TJSException"></exception> /// <exception cref="Kirikiri.Tjs2.VariantException"></exception> protected internal static void SetPropertyIndirect(Variant target, Variant member , Variant param, Dispatch2 objthis, int flags) { if (target.IsObject()) { VariantClosure clo = target.AsObjectClosure(); if (member.IsInteger() != true) { string str = member.AsString(); int hr = clo.PropSet(flags, str, param, clo.mObjThis != null ? clo.mObjThis : objthis ); if (hr < 0) { ThrowFrom_tjs_error(hr, str); } } else { int hr = clo.PropSetByNum(flags, member.AsInteger(), param, clo.mObjThis != null ? clo.mObjThis : objthis); if (hr < 0) { ThrowFrom_tjs_error_num(hr, member.AsInteger()); } } } else { if (target.IsString()) { SetStringProperty(param, target, member); } else { if (target.IsOctet()) { SetOctetProperty(param, target, member); } else { string mes = Error.VariantConvertErrorToObject.Replace("%1", Utils.VariantToReadableString (target)); throw new VariantException(mes); } } } }
/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> /// <exception cref="Kirikiri.Tjs2.TJSException"></exception> /// <exception cref="Kirikiri.Tjs2.CompileException"></exception> private void Eval(Variant val, Dispatch2 objthis, bool resneed) { Variant res = new Variant(); string str = val.AsString(); if (str.Length > 0) { if (resneed) { mBlock.GetTJS().EvalExpression(str, res, objthis, null, 0); } else { mBlock.GetTJS().EvalExpression(str, null, objthis, null, 0); } } if (resneed) { val.Set(res); } }
/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> /// <exception cref="Kirikiri.Tjs2.TJSException"></exception> protected internal static void InstanceOf(Variant name, Variant targ) { // checks instance inheritance. string str = name.AsString(); if (str != null) { int hr = CustomObject.DefaultIsInstanceOf(0, targ, str, null); if (hr < 0) { ThrowFrom_tjs_error(hr, null); } targ.Set((hr == Error.S_TRUE) ? 1 : 0); return; } targ.Set(0); }
/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> private int PutData(Variant val) { // 直近の20个の中で同じものがあるか调べる TODO 别コンパイルにするのなら、全データでチェックするようにした方がいいか int size = mDataArea.Count; int count = size > SEARCH_CONST_VAL_SIZE ? SEARCH_CONST_VAL_SIZE : size; int offset = size - 1; for (int i = 0; i < count; i++) { int pos = offset - i; if (mDataArea[pos].DiscernCompareStrictReal(val)) { return pos; } } Variant v; if (val.IsString()) { v = new Variant(TJS.MapGlobalStringMap(val.AsString())); } else { v = new Variant(val); object o = v.ToJavaObject(); if (o is InterCodeGenerator) { mInterCodeDataArea.AddItem(v); } } mDataArea.AddItem(v); return mDataArea.Count - 1; }
/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> public static object VariantToJavaObject(Variant param, Type type) { if (type.IsPrimitive) { // プリミティブタイプの场合 if (type.Equals(typeof(int))) { return Sharpen.Extensions.ValueOf(param.AsInteger()); } else { if (type.Equals(typeof(double))) { return (param.AsDouble()); } else { if (type.Equals(typeof(bool))) { return Sharpen.Extensions.ValueOf(param.AsInteger() != 0 ? true : false); } else { if (type.Equals(typeof(float))) { return ((float)param.AsDouble()); } else { if (type.Equals(typeof(long))) { return Sharpen.Extensions.ValueOf(param.AsInteger()); } else { if (type.Equals(typeof(char))) { return ((char)param.AsInteger()); } else { if (type.Equals(typeof(byte))) { return (unchecked((byte)param.AsInteger())); } else { if (type.Equals(typeof(short))) { return ((short)param.AsInteger()); } else { // may be Void.TYPE return null; } } } } } } } } } else { if (type.Equals(typeof(string))) { return param.AsString(); } else { if (type.Equals(typeof(ByteBuffer))) { return param.AsOctet(); } else { if (type.Equals(typeof(Variant))) { return param; } else { if (type.Equals(typeof(VariantClosure))) { return param.AsObjectClosure(); } else { if (type.Equals(typeof(Dispatch2))) { return param.AsObject(); } else { if (type.Equals(param.ToJavaObject().GetType())) { return param.ToJavaObject(); } else { // その他 のクラス return null; } } } } } } } }
/// <exception cref="Kirikiri.Tjs2.TJSException"></exception> protected internal override int Process(Variant result, Variant[] param, Dispatch2 objthis) { RegExpNI _this = (RegExpNI)objthis.GetNativeInstance(Kirikiri.Tjs2.RegExpClass.mClassID ); if (_this == null) { return Error.E_NATIVECLASSCRASH; } if (param.Length < 2) { return Error.E_BADPARAMCOUNT; } string target = param[0].AsString(); string to = null; bool func; VariantClosure funcval = null; if (param[1].IsObject() != true) { to = param[1].AsString(); func = false; } else { funcval = param[1].AsObjectClosure(); if (funcval.mObjThis == null) { funcval.mObjThis = objthis; } func = true; } string ret = null; Matcher m = _this.RegEx.Matcher(target); if (func == false) { ret = m.ReplaceAll(to); } else { int hr; VariantClosure clo = new VariantClosure(null, null); Variant funcret = new Variant(); Variant arrayval = new Variant(clo); Variant[] args = new Variant[1]; args[0] = arrayval; int size = target.Length; ret = string.Empty; for (int i = 0; i < size; ) { if (m.Find(i)) { ret += Sharpen.Runtime.Substring(target, i, m.Start()); Dispatch2 array = Kirikiri.Tjs2.RegExpClass.GetResultArray(true, _this, m); clo.Set(array, array); hr = funcval.FuncCall(0, null, funcret, args, null); if (hr >= 0) { ret += funcret.AsString(); } i = m.End(); } else { break; } } } if (result != null) { result.Set(ret); } return Error.S_OK; }
/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> public static string VariantToReadableString(Variant val, int maxlen) { string ret = null; if (val == null || val.IsVoid()) { ret = "(void)"; } else { if (val.IsInteger()) { ret = "(int)" + val.AsString(); } else { if (val.IsReal()) { ret = "(real)" + val.AsString(); } else { if (val.IsString()) { ret = "(string)\"" + LexBase.EscapeC(val.AsString()) + "\""; } else { if (val.IsOctet()) { ret = "(octet)<% " + Variant.OctetToListString(val.AsOctet()) + " %>"; } else { if (val.IsObject()) { VariantClosure c = (VariantClosure)val.AsObjectClosure(); StringBuilder str = new StringBuilder(128); str.Append("(object)"); str.Append('('); if (c.mObject != null) { str.Append('['); if (c.mObject is NativeClass) { str.Append(((NativeClass)c.mObject).GetClassName()); } else { if (c.mObject is InterCodeObject) { str.Append(((InterCodeObject)c.mObject).GetName()); } else { if (c.mObject is CustomObject) { string name = ((CustomObject)c.mObject).GetClassNames(); if (name != null) { str.Append(name); } else { str.Append(c.mObject.GetType().FullName); } } else { str.Append(c.mObject.GetType().FullName); } } } str.Append(']'); } else { str.Append("0x00000000"); } if (c.mObjThis != null) { str.Append('['); if (c.mObjThis is NativeClass) { str.Append(((NativeClass)c.mObjThis).GetClassName()); } else { if (c.mObjThis is InterCodeObject) { str.Append(((InterCodeObject)c.mObjThis).GetName()); } else { if (c.mObjThis is CustomObject) { string name = ((CustomObject)c.mObjThis).GetClassNames(); if (name != null) { str.Append(name); } else { str.Append(c.mObjThis.GetType().FullName); } } else { str.Append(c.mObjThis.GetType().FullName); } } } str.Append(']'); } else { str.Append(":0x00000000"); } str.Append(')'); ret = str.ToString(); } else { // native object ? ret = "(octet) [" + val.GetType().FullName + "]"; } } } } } } if (ret != null) { if (ret.Length > maxlen) { return Sharpen.Runtime.Substring(ret, 0, maxlen); } else { return ret; } } return string.Empty; }
/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> public override int ClassInstanceInfo(int flag, int num, Variant value) { switch (flag) { case Interface.CII_ADD: { // add value string name = value.AsString(); // デバッグ系はなし //if( objectHashMapEnabled() && mClassNames.size() == 0) // objectHashSetType( this, "instance of class " + name ); // First class name is used for the object classname // because the order of the class name // registration is from descendant to ancestor. mClassNames.AddItem(name); return Error.S_OK; } case Interface.CII_GET: { // get value if (num >= mClassNames.Count) { return Error.E_FAIL; } value.Set(mClassNames[num]); return Error.S_OK; } case Interface.CII_SET_FINALIZE: { // set 'finalize' method name mfinalize_name = value.AsString(); mCallFinalize = mfinalize_name.Length > 0; return Error.S_OK; } case Interface.CII_SET_MISSING: { // set 'missing' method name mmissing_name = value.AsString(); mCallMissing = mmissing_name.Length > 0; return Error.S_OK; } } return Error.E_NOTIMPL; }
/// <exception cref="Kirikiri.Tjs2.TJSException"></exception> /// <exception cref="Kirikiri.Tjs2.VariantException"></exception> private static void GetStringProperty(Variant result, Variant str, int n) { // processes properties toward strings. string s = str.AsString(); int len = s.Length; if (n == len) { result.Set(string.Empty); return; } if (n < 0 || n > len) { throw new TJSException(Error.RangeError); } result.Set(Sharpen.Runtime.Substring(s, n, n + 1)); return; }
/// <exception cref="Kirikiri.Tjs2.TJSException"></exception> /// <exception cref="Kirikiri.Tjs2.VariantException"></exception> protected internal static void OperatePropertyIndirect0(VariantClosure clo, Variant name, Variant result, Dispatch2 objthis, int ope) { if (name.IsInteger() != true) { string str = name.AsString(); int hr = clo.Operation(ope, str, result, null, clo.mObjThis != null ? clo.mObjThis : objthis); if (hr < 0) { ThrowFrom_tjs_error(hr, str); } } else { int hr = clo.OperationByNum(ope, name.AsInteger(), result, null, clo.mObjThis != null ? clo.mObjThis : objthis); if (hr < 0) { ThrowFrom_tjs_error_num(hr, name.AsInteger()); } } }
/// <exception cref="Kirikiri.Tjs2.TJSException"></exception> /// <exception cref="Kirikiri.Tjs2.VariantException"></exception> public virtual void Randomize(Variant[] param) { if (param.Length == 0) { // parametor not given if (mRandomBits128 != null) { // another random generator is given //tjs_uint8 buf[32]; //unsigned long tmp[32]; ByteBuffer buf = ByteBuffer.AllocateDirect(32); mRandomBits128.GetRandomBits128(buf, 0); mRandomBits128.GetRandomBits128(buf, 16); int[] tmp = new int[32]; for (int i = 0; i < 32; i++) { long num = (long)buf.Get(i) + ((long)buf.Get(i) << 8) + ((long)buf.Get(1) << 16) + ((long)buf.Get(i) << 24); tmp[i] = (int)(num > int.MaxValue ? num - unchecked((long)(0x100000000L)) : num); } if (mGenerator != null) { mGenerator = null; } mGenerator = new MersenneTwister(tmp); } else { if (mGenerator != null) { mGenerator = null; } mGenerator = new MersenneTwister(Runtime.CurrentTimeMillis()); } } else { if (param.Length >= 1) { if (param[0].IsObject()) { MersenneTwisterData data = null; try { // may be a reconstructible information VariantClosure clo = param[0].AsObjectClosure(); if (clo.mObject == null) { throw new TJSException(Error.NullAccess); } string state; Variant val = new Variant(); data = new MersenneTwisterData(); // get state array //TJSThrowFrom_tjs_error int hr = clo.PropGet(Interface.MEMBERMUSTEXIST, "state", val, null); if (hr < 0) { Error.ThrowFrom_tjs_error(hr, null); } state = val.AsString(); if (state.Length != MT_N * 8) { throw new TJSException(Error.NotReconstructiveRandomizeData); } int p = 0; for (int i = 0; i < MT_N; i++) { long n = 0; int tmp; for (int j = 0; j < 8; j++) { int c = state[p + j]; tmp = -1; if (c >= '0' && c <= '9') { n = c - '0'; } else { if (c >= 'a' && c <= 'f') { n = c - 'a' + 10; } else { if (c >= 'A' && c <= 'F') { n = c - 'A' + 10; } } } if (tmp == -1) { throw new TJSException(Error.NotReconstructiveRandomizeData); } else { n <<= 4; n += tmp; } } p += 8; data.state.Put(i, n & unchecked((long)(0xffffffffL))); } // get other members hr = clo.PropGet(Interface.MEMBERMUSTEXIST, "left", val, null); if (hr < 0) { Error.ThrowFrom_tjs_error(hr, null); } data.left = val.AsInteger(); hr = clo.PropGet(Interface.MEMBERMUSTEXIST, "next", val, null); data.next = val.AsInteger(); if (mGenerator != null) { mGenerator = null; } mGenerator = new MersenneTwister(data); } catch (VariantException) { data = null; throw new TJSException(Error.NotReconstructiveRandomizeData); } catch (TJSException) { data = null; throw new TJSException(Error.NotReconstructiveRandomizeData); } data = null; } else { // 64bitじゃなくて、32bit にしてしまっている。实用上问题あれば修正。 int n = param[0].AsInteger(); int[] tmp = new int[1]; tmp[0] = n; if (mGenerator != null) { mGenerator = null; } mGenerator = new MersenneTwister(tmp); } } } }