Ejemplo n.º 1
0
        public Compiler()
        {
            _engine = new ScriptEngine("jscript");

            Debug.Assert(!string.IsNullOrWhiteSpace(LoadResource("sandbox.js")));
            Debug.Assert(!string.IsNullOrWhiteSpace(LoadResource("compile.js")));
            Debug.Assert(!string.IsNullOrWhiteSpace(LoadResource("Scripts", "ember.js")));
            Debug.Assert(!string.IsNullOrWhiteSpace(LoadResource("Scripts", "handlebars-1.0.rc.2.js")));

            _vm = _engine.Parse(
                LoadResource("sandbox.js")
                + LoadResource("Scripts", "handlebars-1.0.rc.2.js")
                + LoadResource("Scripts", "ember.js")
                + LoadResource("compile.js"));
        }
        private object Parse(string text, bool expression)
        {
            const string varName = "x___";
            System.Runtime.InteropServices.ComTypes.EXCEPINFO exceptionInfo;
            object result;

            _engine.SetScriptState(ScriptState.Connected);

            ScriptText flags = ScriptText.None;
            if (expression)
            {
                flags |= ScriptText.IsExpression;
            }

            try
            {
                // immediate expression computation seems to work only for 64-bit
                // so hack something for 32-bit...
                if (_parse32 != null)
                {
                    if (expression)
                    {
                        // should work for jscript & vbscript at least...
                        text = varName + "=" + text;
                    }
                    _parse32.ParseScriptText(text, null, null, null, IntPtr.Zero, 0, flags, out result, out exceptionInfo);
                }
                else
                {
                    _parse64.ParseScriptText(text, null, null, null, IntPtr.Zero, 0, flags, out result, out exceptionInfo);
                }
            }
            catch
            {
                if (_site._lastException != null)
                    throw _site._lastException;

                throw;
            }

            IntPtr dispatch;
            if (expression)
            {
                // continue  out 32-bit hack...
                if (_parse32 != null)
                {
                    _engine.GetScriptDispatch(null, out dispatch);
                    object dp = Marshal.GetObjectForIUnknown(dispatch);
                    try
                    {
                        return dp.GetType().InvokeMember(varName, BindingFlags.GetProperty, null, dp, null);
                    }
                    catch
                    {
                        if (_site._lastException != null)
                            throw _site._lastException;

                        throw;
                    }
                }
                return result;
            }

            _engine.GetScriptDispatch(null, out dispatch);
            ParsedScript parsed = new ParsedScript(this, dispatch);
            return parsed;
        }