/// <exception cref="Kirikiri.Tjs2.TJSException"></exception> private void ExportByteCode(BinaryStream output) { byte[] filetag = FILE_TAG; byte[] codetag = new byte[] { (byte)('T'), (byte)('J'), (byte)('S'), (byte)('2') }; byte[] objtag = new byte[] { (byte)('O'), (byte)('B'), (byte)('J'), (byte)('S') }; byte[] datatag = new byte[] { (byte)('D'), (byte)('A'), (byte)('T'), (byte)('A') }; int count = mInterCodeGeneratorList.Count; AList<ByteBuffer> objarray = new AList<ByteBuffer>(count * 2); ConstArrayData constarray = new ConstArrayData(); int objsize = 0; for (int i = 0; i < count; i++) { InterCodeGenerator obj = mInterCodeGeneratorList[i]; ByteBuffer buf = obj.ExportByteCode(this, constarray); objarray.AddItem(buf); objsize += buf.Capacity() + TAG_SIZE + CHUNK_SIZE_LEN; } // tag + size objsize += TAG_SIZE + CHUNK_SIZE_LEN + 4 + 4; // OBJS tag + size + toplevel + count ByteBuffer dataarea = constarray.ExportBuffer(); int datasize = dataarea.Capacity() + TAG_SIZE + CHUNK_SIZE_LEN; // DATA tag + size int filesize = objsize + datasize + FILE_TAG_SIZE + CHUNK_SIZE_LEN; // TJS2 tag + file size byte[] filesizearray = new byte[] { unchecked((byte)(filesize & unchecked((int)(0xff )))), unchecked((byte)(((int)(((uint)filesize) >> 8)) & unchecked((int)(0xff)))) , unchecked((byte)(((int)(((uint)filesize) >> 16)) & unchecked((int)(0xff)))), unchecked( (byte)(((int)(((uint)filesize) >> 24)) & unchecked((int)(0xff)))) }; byte[] datasizearray = new byte[] { unchecked((byte)(datasize & unchecked((int)(0xff )))), unchecked((byte)(((int)(((uint)datasize) >> 8)) & unchecked((int)(0xff)))) , unchecked((byte)(((int)(((uint)datasize) >> 16)) & unchecked((int)(0xff)))), unchecked( (byte)(((int)(((uint)datasize) >> 24)) & unchecked((int)(0xff)))) }; byte[] objsizearray = new byte[] { unchecked((byte)(objsize & unchecked((int)(0xff )))), unchecked((byte)(((int)(((uint)objsize) >> 8)) & unchecked((int)(0xff)))), unchecked((byte)(((int)(((uint)objsize) >> 16)) & unchecked((int)(0xff)))), unchecked( (byte)(((int)(((uint)objsize) >> 24)) & unchecked((int)(0xff)))) }; byte[] objcountarray = new byte[] { unchecked((byte)(count & unchecked((int)(0xff )))), unchecked((byte)(((int)(((uint)count) >> 8)) & unchecked((int)(0xff)))), unchecked( (byte)(((int)(((uint)count) >> 16)) & unchecked((int)(0xff)))), unchecked((byte) (((int)(((uint)count) >> 24)) & unchecked((int)(0xff)))) }; int toplevel = -1; if (mTopLevelGenerator != null) { toplevel = GetCodeIndex(mTopLevelGenerator); } byte[] toparray = new byte[] { unchecked((byte)(toplevel & unchecked((int)(0xff)) )), unchecked((byte)(((int)(((uint)toplevel) >> 8)) & unchecked((int)(0xff)))), unchecked((byte)(((int)(((uint)toplevel) >> 16)) & unchecked((int)(0xff)))), unchecked( (byte)(((int)(((uint)toplevel) >> 24)) & unchecked((int)(0xff)))) }; output.Write(filetag); output.Write(filesizearray); output.Write(datatag); output.Write(datasizearray); output.Write(dataarea); output.Write(objtag); output.Write(objsizearray); output.Write(toparray); output.Write(objcountarray); for (int i_1 = 0; i_1 < count; i_1++) { ByteBuffer buf = objarray[i_1]; int size = buf.Capacity(); byte[] bufsizearray = new byte[] { unchecked((byte)(size & unchecked((int)(0xff)) )), unchecked((byte)(((int)(((uint)size) >> 8)) & unchecked((int)(0xff)))), unchecked( (byte)(((int)(((uint)size) >> 16)) & unchecked((int)(0xff)))), unchecked((byte)( ((int)(((uint)size) >> 24)) & unchecked((int)(0xff)))) }; output.Write(codetag); output.Write(bufsizearray); output.Write(buf); } output.Close(); output = null; objarray.Clear(); objarray = null; constarray = null; dataarea = null; }
/// <exception cref="Kirikiri.Tjs2.CompileException"></exception> /// <exception cref="Kirikiri.Tjs2.VariantException"></exception> /// <exception cref="Kirikiri.Tjs2.TJSException"></exception> public virtual void Compile(string text, bool isexpression, bool isresultneeded, BinaryStream output) { if (text == null) { return; } if (text.Length == 0) { return; } mScript = text; Parse(text, isexpression, isresultneeded); // ここでバイトコード出力する //if( mName != null && mName.endsWith(".tjs") ) { // String filename = mName.substring(0,mName.length()-4) + ".tjb"; // exportByteCode(filename); //} ExportByteCode(output); }
/// <exception cref="Kirikiri.Tjs2.TJSException"></exception> public virtual void LoadByteCode(Variant result, Dispatch2 context, string name, BinaryStream input) { ByteCodeLoader loader = new ByteCodeLoader(); ScriptBlock block = loader.ReadByteCode(this, name, input); block.ExecuteTopLevel(result, context); if (block.GetContextCount() == 0) { RemoveScriptBlock(block); } block = null; }
/// <exception cref="Kirikiri.Tjs2.TJSException"></exception> public virtual ScriptBlock ReadByteCode(TJS owner, string name, BinaryStream input ) { try { int size = (int)input.GetSize(); if (mReadBuffer == null || mReadBuffer.Length < size) { int buflen = size < MIN_READ_BUFFER_SIZE ? MIN_READ_BUFFER_SIZE : size; mReadBuffer = new byte[buflen]; } byte[] databuff = mReadBuffer; input.Read(databuff); input.Close(); input = null; // TJS2 int tag = (databuff[0] & unchecked((int)(0xff))) | (databuff[1] & unchecked((int) (0xff))) << 8 | (databuff[2] & unchecked((int)(0xff))) << 16 | (databuff[3] & unchecked( (int)(0xff))) << 24; if (tag != FILE_TAG_LE) { return null; } // 100'\0' int ver = (databuff[4] & unchecked((int)(0xff))) | (databuff[5] & unchecked((int) (0xff))) << 8 | (databuff[6] & unchecked((int)(0xff))) << 16 | (databuff[7] & unchecked( (int)(0xff))) << 24; if (ver != VER_TAG_LE) { return null; } int filesize = (databuff[8] & unchecked((int)(0xff))) | (databuff[9] & unchecked( (int)(0xff))) << 8 | (databuff[10] & unchecked((int)(0xff))) << 16 | (databuff[11 ] & unchecked((int)(0xff))) << 24; if (filesize != size) { return null; } //// DATA tag = (databuff[12] & unchecked((int)(0xff))) | (databuff[13] & unchecked((int)(0xff ))) << 8 | (databuff[14] & unchecked((int)(0xff))) << 16 | (databuff[15] & unchecked( (int)(0xff))) << 24; if (tag != DATA_TAG_LE) { return null; } size = (databuff[16] & unchecked((int)(0xff))) | (databuff[17] & unchecked((int)( 0xff))) << 8 | (databuff[18] & unchecked((int)(0xff))) << 16 | (databuff[19] & unchecked( (int)(0xff))) << 24; ReadDataArea(databuff, 20, size); int offset = 12 + size; // これがデータエリア后の位置 // OBJS tag = (databuff[offset] & unchecked((int)(0xff))) | (databuff[offset + 1] & unchecked( (int)(0xff))) << 8 | (databuff[offset + 2] & unchecked((int)(0xff))) << 16 | (databuff [offset + 3] & unchecked((int)(0xff))) << 24; offset += 4; if (tag != OBJ_TAG_LE) { return null; } //int objsize = ibuff.get(); int objsize = (databuff[offset] & unchecked((int)(0xff))) | (databuff[offset + 1] & unchecked((int)(0xff))) << 8 | (databuff[offset + 2] & unchecked((int)(0xff)) ) << 16 | (databuff[offset + 3] & unchecked((int)(0xff))) << 24; offset += 4; ScriptBlock block = new ScriptBlock(owner, name, 0, null, null); ReadObjects(block, databuff, offset, objsize); return block; } finally { if (mDeleteBuffer) { mReadBuffer = null; mByteArray = null; mShortArray = null; mIntArray = null; mLongArray = null; mDoubleArray = null; mDoubleTmpArray = null; mStringArray = null; mByteBufferArray = null; mObjectsCache.Release(); mVariantTypeData = null; } } }
/// <exception cref="Kirikiri.Tjs2.CompileException"></exception> /// <exception cref="Kirikiri.Tjs2.VariantException"></exception> /// <exception cref="Kirikiri.Tjs2.TJSException"></exception> public virtual void CompileScript(string script, string name, int lineofs, bool isresultneeded , BinaryStream output) { Compiler compiler = new Compiler(this); if (name != null) { compiler.SetName(name, lineofs); } compiler.Compile(script, false, isresultneeded, output); compiler = null; }