<commit_before>/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is [ActiveScripting implemented with Tamarin].
 *
 * The Initial Developer of the Original Code is Mozilla Corp.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   <NAME>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

// hackery - for now, this file is "built" via:
// %java -ea -DAS3 -DAVMPLUS -classpath ../utils/asc.jar macromedia.asc.embedding.ScriptCompiler -abcfuture -builtin -d -import ../core/builtin.abc -import ../esc/bin/esc-core.es.abc -import ../esc/bin/eval-support.es.abc -out axtoplevel mscom.as Domain.as ../shell/ByteArray.as && move /y ..\shell\axtoplevel.* .
// Note the '-d' will include debug info which is handy in the early days...

package axtam 
{
	// should possibly be private...
	public class System
	{
		public native static function getAvmplusVersion():String
		public native static function write(s:String):void
		public native static function trace(a:Array):void
		public native static function debugger():void
		public native static function isDebugger():Boolean
		public native static function nativeDebugBreak():void
	}
}

package 
{
	// a global 'print' statement...
	public function print(...s)
	{
		axtam.System.trace(s)
	}

	namespace ESC = "ESC"; // implicit namespace from esc-env.ast

	// 'eval' support is still a work in progress.
	public function eval(...rest) {} // just needs to exist!!
}

// a clone of the File class from shell/toplevel.as
package avmplus {
	public class File
	{
		public native static function exists(filename:String):String;
		public native static function read(filename:String):String;
		public native static function write(filename:String, data:String):void;
	}
}

package axtam.com {
	// Its not clear if we should subclass the standard 'Error', but given
	// it has its own concept of 'Message' and 'ID', it doesn't seem a good fit.
	// Maybe a new base-class should be introduced?
	public dynamic class Error
	{
		public static const S_OK = 0
		public static const S_FALSE = 1

		public static const E_NOTIMPL = 0x80004001
		public static const E_INVALIDARG = 0x80070057
		public static const E_UNEXPECTED = 0x8000ffff
		public static const E_FAIL = 0x80004005
		public static const E_NOINTERFACE = 0x80000004

		public var hresult: int
		public var excepinfo: EXCEPINFO
		function Error(hresult, excepinfo:EXCEPINFO = null) {
			this.hresult = hresult;
			this.excepinfo = excepinfo;
		}

		prototype.toString = function():String
		{
			var excepinfo:EXCEPINFO = this.excepinfo;
			var e:axtam.com.Error = this
			var code:uint;
			var msg:String;
			code = e.hresult;
			if (excepinfo) {
				// our hresult is almost certainly DISP_E_EXCEPTION, so
				// either of the ones in the excep info are likely to be better
				if (excepinfo.code)
					code = excepinfo.code
				else if (excepinfo.scode)
					code = excepinfo.scode
			}
			if (excepinfo && excepinfo.description) {
				msg = excepinfo.description + " (source:" + excepinfo.source + ", help:" + excepinfo.helpFile + ", helpContext:" + excepinfo.helpContext + ")";
			} else {
				msg = ProviderError.getErrorMessage(code);
			}
			var hv:String = code >= 0 ? code.toString(16) : (code+0x100000000).toString(16)
			return "COM Error 0x" + hv + ": " + msg
		}
		_setPropertyIsEnumerable(prototype, "toString", false);

		// *sob* - I can't get this to work without tamarin throwing exceptions.
		//public native static function getErrorMessage(index:int):String;

		public static function throwError(hresult:int, ... rest)
		{
			// todo: rich error support?
			throw new axtam.com.ProviderError(hresult);
		}

	}
	// This is the actual class that is thrown by script code (see throwError above)
	// When exceptions of this class are thrown, they are not treated as "unhandled",
	// but simply indicate that script code wants a COM error returned.
	public dynamic class ProviderError extends axtam.com.Error {
		function ProviderError(hresult, excepinfo:EXCEPINFO = null) {
			super(hresult, excepinfo)
		}
		// *sob* - see above
		public native static function getErrorMessage(index:int):String;
	}

	// This is the class that is thrown by *native* code when a COM object you are using
	// (such as Window) returns a COM error code.  These are never thrown by script code,
	// and are treated as "unhandled" when the occur - eg, a traceback is printed, debugger
	// semantics followed, etc.
	public dynamic class ConsumerError extends axtam.com.Error {
		function ConsumerError(hresult, excepinfo:EXCEPINFO = null) {
			super(hresult, excepinfo)
		}
		// *sob* - see above
		public native static function getErrorMessage(index:int):String;
	}

	public class EXCEPINFO {
		public native function get code():uint
		public native function get source():String
		public native function get description():String
		public native function get helpFile():String
		public native function get helpContext():uint
		public native function get scode():uint
	}

	// constants go directly in the axtam.com package.
	public const SCRIPTINFO_IUNKNOWN = 0x00000001
	public const SCRIPTINFO_ITYPEINFO = 0x00000002

	public const SCRIPTITEM_ISVISIBLE = 0x00000002
	public const SCRIPTITEM_ISSOURCE = 0x00000004
	public const SCRIPTITEM_GLOBALMEMBERS = 0x00000008
	public const SCRIPTITEM_ISPERSISTENT = 0x00000040
	public const SCRIPTITEM_CODEONLY = 0x00000200
	public const SCRIPTITEM_NOCODE = 0x00000400

	public const SCRIPTSTATE_UNINITIALIZED       = 0
	public const SCRIPTSTATE_INITIALIZED         = 5
	public const SCRIPTSTATE_STARTED             = 1
	public const SCRIPTSTATE_CONNECTED           = 2
	public const SCRIPTSTATE_DISCONNECTED        = 3
	public const SCRIPTSTATE_CLOSED              = 4
}

package axtam.com.consumer {
	// scripting interfaces we consume in AS (ie, implemented externally)
	// Each method listed here corresponds to a native method in the engine, 
	// which inturn delegates to the real COM object.
	// Note that some of these classes have no scriptable methods, but they
	// are used internally
	public class IUnknown
	{
		public function toString():String
		{
			return "[COM Object]"
		}
	}

	// The rest of these 'derive' from IUnknown...
	// An IDispatch object does *not* expose IDispatch methods - instead,
	// it uses IDispatch to provide normal "object.property" style access.
	// This is primarily due to a lack of "catch-alls" and effort required in
	// managing the args used by the native functions.
	// It is "dynamic" as it supports 'expandos'
	public dynamic class IDispatch extends IUnknown
	{
	}

	public class ITypeInfo extends IUnknown
	{
	}

	public class IActiveScriptSite
	{
		// somewhat like GetItemInfo() - but gets both IDispatch and ITypelib,
		// and wraps them in a ScriptObject which can be used from script.
		public native function GetItemInfo(name:String, flags:uint):Array
		public native function GetDocVersionString(): String
		public native function OnStateChange(state:uint):int
		public native function OnEnterScript():int
		public native function OnLeaveScript():int
	}
}

package axtam.com.provider {
	// scripting interfaces provided by AS (ie, for consumption by external
	// code).  As such, there are no classes available for use by JS!
	// A couple of functions are used to *create* such objects, but they
	// are returned via an axtam.com.consumer.IUnknown (which is wrapping
	// the object we provide)
	public native function createDispatchProvider(ob:Object): axtam.com.consumer.IUnknown;
}

package {

	import flash.utils.*; // for our ByteArray clone - either it should die, or we rename the package in our clone!

	public function compileString(str, fname:String = "", startLineNumber:int = 0)
	{
// Bug 432242 is adding a 'compileStringToBytes' function, but its not checked in
// yet - so reimplement it ourselves.  Note that line numbers will be broken until
// that bug is fixed.
//		use namespace ESC;
//		var bytes = ESC::compileStringToBytes(str, fname, startLineNumber+1);
		var bytes;
		ESC::compile( function () { return str },
					  function (abc) { bytes = abc.getBytes() },
					  fname);
		return bytes;
	}
}

package {

	final class NamedItem {
		public var domain:axtam.Domain
		public var host_dispatch:axtam.com.consumer.IDispatch
		public var script_dispatch:axtam.com.consumer.IDispatch
	}

	class ScriptEngine {
		// A hashtable of NamedItem() objects, with a null key being the global object.
		public var namedItems;
		public var codeBlocks; // code blocks "pushed" but not yet executed.
		public var state:int;
		public var site; // IUnknownConsumer

		public function ScriptEngine()
		{<commit_msg>Update to use the ESC api in bug 432242<commit_after>/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is [ActiveScripting implemented with Tamarin].
 *
 * The Initial Developer of the Original Code is Mozilla Corp.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   <NAME>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

// hackery - for now, this file is "built" via:
// %java -ea -DAS3 -DAVMPLUS -classpath ../utils/asc.jar macromedia.asc.embedding.ScriptCompiler -abcfuture -builtin -d -import ../core/builtin.abc -import ../esc/bin/esc-core.es.abc -import ../esc/bin/eval-support.es.abc -out axtoplevel mscom.as Domain.as ../shell/ByteArray.as && move /y ..\shell\axtoplevel.* .
// Note the '-d' will include debug info which is handy in the early days...

package axtam 
{
	// should possibly be private...
	public class System
	{
		public native static function getAvmplusVersion():String
		public native static function write(s:String):void
		public native static function trace(a:Array):void
		public native static function debugger():void
		public native static function isDebugger():Boolean
		public native static function nativeDebugBreak():void
	}
}

package 
{
	// a global 'print' statement...
	public function print(...s)
	{
		axtam.System.trace(s)
	}

	namespace ESC = "ESC"; // implicit namespace from esc-env.ast

	// 'eval' support is still a work in progress.
	public function eval(...rest) {} // just needs to exist!!
}

// a clone of the File class from shell/toplevel.as
package avmplus {
	public class File
	{
		public native static function exists(filename:String):String;
		public native static function read(filename:String):String;
		public native static function write(filename:String, data:String):void;
	}
}

package axtam.com {
	// Its not clear if we should subclass the standard 'Error', but given
	// it has its own concept of 'Message' and 'ID', it doesn't seem a good fit.
	// Maybe a new base-class should be introduced?
	public dynamic class Error
	{
		public static const S_OK = 0
		public static const S_FALSE = 1

		public static const E_NOTIMPL = 0x80004001
		public static const E_INVALIDARG = 0x80070057
		public static const E_UNEXPECTED = 0x8000ffff
		public static const E_FAIL = 0x80004005
		public static const E_NOINTERFACE = 0x80000004

		public var hresult: int
		public var excepinfo: EXCEPINFO
		function Error(hresult, excepinfo:EXCEPINFO = null) {
			this.hresult = hresult;
			this.excepinfo = excepinfo;
		}

		prototype.toString = function():String
		{
			var excepinfo:EXCEPINFO = this.excepinfo;
			var e:axtam.com.Error = this
			var code:uint;
			var msg:String;
			code = e.hresult;
			if (excepinfo) {
				// our hresult is almost certainly DISP_E_EXCEPTION, so
				// either of the ones in the excep info are likely to be better
				if (excepinfo.code)
					code = excepinfo.code
				else if (excepinfo.scode)
					code = excepinfo.scode
			}
			if (excepinfo && excepinfo.description) {
				msg = excepinfo.description + " (source:" + excepinfo.source + ", help:" + excepinfo.helpFile + ", helpContext:" + excepinfo.helpContext + ")";
			} else {
				msg = ProviderError.getErrorMessage(code);
			}
			var hv:String = code >= 0 ? code.toString(16) : (code+0x100000000).toString(16)
			return "COM Error 0x" + hv + ": " + msg
		}
		_setPropertyIsEnumerable(prototype, "toString", false);

		// *sob* - I can't get this to work without tamarin throwing exceptions.
		//public native static function getErrorMessage(index:int):String;

		public static function throwError(hresult:int, ... rest)
		{
			// todo: rich error support?
			throw new axtam.com.ProviderError(hresult);
		}

	}
	// This is the actual class that is thrown by script code (see throwError above)
	// When exceptions of this class are thrown, they are not treated as "unhandled",
	// but simply indicate that script code wants a COM error returned.
	public dynamic class ProviderError extends axtam.com.Error {
		function ProviderError(hresult, excepinfo:EXCEPINFO = null) {
			super(hresult, excepinfo)
		}
		// *sob* - see above
		public native static function getErrorMessage(index:int):String;
	}

	// This is the class that is thrown by *native* code when a COM object you are using
	// (such as Window) returns a COM error code.  These are never thrown by script code,
	// and are treated as "unhandled" when the occur - eg, a traceback is printed, debugger
	// semantics followed, etc.
	public dynamic class ConsumerError extends axtam.com.Error {
		function ConsumerError(hresult, excepinfo:EXCEPINFO = null) {
			super(hresult, excepinfo)
		}
		// *sob* - see above
		public native static function getErrorMessage(index:int):String;
	}

	public class EXCEPINFO {
		public native function get code():uint
		public native function get source():String
		public native function get description():String
		public native function get helpFile():String
		public native function get helpContext():uint
		public native function get scode():uint
	}

	// constants go directly in the axtam.com package.
	public const SCRIPTINFO_IUNKNOWN = 0x00000001
	public const SCRIPTINFO_ITYPEINFO = 0x00000002

	public const SCRIPTITEM_ISVISIBLE = 0x00000002
	public const SCRIPTITEM_ISSOURCE = 0x00000004
	public const SCRIPTITEM_GLOBALMEMBERS = 0x00000008
	public const SCRIPTITEM_ISPERSISTENT = 0x00000040
	public const SCRIPTITEM_CODEONLY = 0x00000200
	public const SCRIPTITEM_NOCODE = 0x00000400

	public const SCRIPTSTATE_UNINITIALIZED       = 0
	public const SCRIPTSTATE_INITIALIZED         = 5
	public const SCRIPTSTATE_STARTED             = 1
	public const SCRIPTSTATE_CONNECTED           = 2
	public const SCRIPTSTATE_DISCONNECTED        = 3
	public const SCRIPTSTATE_CLOSED              = 4
}

package axtam.com.consumer {
	// scripting interfaces we consume in AS (ie, implemented externally)
	// Each method listed here corresponds to a native method in the engine, 
	// which inturn delegates to the real COM object.
	// Note that some of these classes have no scriptable methods, but they
	// are used internally
	public class IUnknown
	{
		public function toString():String
		{
			return "[COM Object]"
		}
	}

	// The rest of these 'derive' from IUnknown...
	// An IDispatch object does *not* expose IDispatch methods - instead,
	// it uses IDispatch to provide normal "object.property" style access.
	// This is primarily due to a lack of "catch-alls" and effort required in
	// managing the args used by the native functions.
	// It is "dynamic" as it supports 'expandos'
	public dynamic class IDispatch extends IUnknown
	{
	}

	public class ITypeInfo extends IUnknown
	{
	}

	public class IActiveScriptSite
	{
		// somewhat like GetItemInfo() - but gets both IDispatch and ITypelib,
		// and wraps them in a ScriptObject which can be used from script.
		public native function GetItemInfo(name:String, flags:uint):Array
		public native function GetDocVersionString(): String
		public native function OnStateChange(state:uint):int
		public native function OnEnterScript():int
		public native function OnLeaveScript():int
	}
}

package axtam.com.provider {
	// scripting interfaces provided by AS (ie, for consumption by external
	// code).  As such, there are no classes available for use by JS!
	// A couple of functions are used to *create* such objects, but they
	// are returned via an axtam.com.consumer.IUnknown (which is wrapping
	// the object we provide)
	public native function createDispatchProvider(ob:Object): axtam.com.consumer.IUnknown;
}

package {

	import flash.utils.*; // for our ByteArray clone - either it should die, or we rename the package in our clone!

	public function compileString(str, fname:String = "", startLineNumber:int = 0)
	{
		// NOTE: This requires the patch to bug 432242 (and by the time this
		// comment is stale, we should remove compileString() completely and
		// inline it!
		return ESC::compileStringToBytes(str, fname, startLineNumber+1);
	}
}

package {

	final class NamedItem {
		public var domain:axtam.Domain
		public var host_dispatch:axtam.com.consumer.IDispatch
		public var script_dispatch:axtam.com.consumer.IDispatch
	}

	class ScriptEngine {
		// A hashtable of NamedItem() objects, with a null key being the global object.
		public var namedItems;
		public var codeBlocks; // code blocks "pushed" but not yet executed.
		public var state:int;
		public var site; // IUnknownConsumer

		public function ScriptEngine()
		{
{'fraction_non_alphanumeric': 0.08315329347334573, 'fraction_numerical': 0.016479927649098124, 'mean_word_length': 3.6766917293233083, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1307464', 'n_tokens_mistral': 6443, 'n_tokens_neox': 5741, 'n_words': 2340}