<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Решить пример, записанный строкой?]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=18302&amp;type=atom" />
	<updated>2024-09-21T18:31:46Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=18302</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Решить пример, записанный строкой?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=161630#p161630" />
			<content type="html"><![CDATA[<p>Если нужно возведение в степень через js движок, то ComObjCreate(&quot;htmlfile&quot;) не подойдет.<br />Можно так:<br /></p><div class="codebox"><pre><code>js := new JsRT.Edge
MsgBox % js.Eval(&quot;3**3&quot;)


/*
 *  ActiveScript for AutoHotkey v1.1
 *
 *  Provides an interface to Active Scripting languages like VBScript and JScript,
 *  without relying on Microsoft&#039;s ScriptControl, which is not available to 64-bit
 *  programs.
 *
 *  License: Use, modify and redistribute without limitation, but at your own risk.
 */
class ActiveScript extends ActiveScript._base
{
    __New(Language)
    {
        if this._script := ComObjCreate(Language, ActiveScript.IID)
            this._scriptParse := ComObjQuery(this._script, ActiveScript.IID_Parse)
        if !this._scriptParse
            throw Exception(&quot;Invalid language&quot;, -1, Language)
        this._site := new ActiveScriptSite(this)
        this._SetScriptSite(this._site.ptr)
        this._InitNew()
        this._objects := {}
        this.Error := &quot;&quot;
        this._dsp := this._GetScriptDispatch()  ; Must be done last.
        try
            if this.ScriptEngine() = &quot;JScript&quot;
                this.SetJScript58()
    }

    SetJScript58()
    {
        static IID_IActiveScriptProperty := &quot;{4954E0D0-FBC7-11D1-8410-006008C3FBFC}&quot;
        if !prop := ComObjQuery(this._script, IID_IActiveScriptProperty)
            return false
        VarSetCapacity(var, 24, 0), NumPut(2, NumPut(3, var, &quot;short&quot;) + 6)
        hr := DllCall(NumGet(NumGet(prop+0)+4*A_PtrSize), &quot;ptr&quot;, prop, &quot;uint&quot;, 0x4000
            , &quot;ptr&quot;, 0, &quot;ptr&quot;, &amp;var), ObjRelease(prop)
        return hr &gt;= 0
    }
    
    Eval(Code)
    {
        pvar := NumGet(ComObjValue(arr:=ComObjArray(0xC,1)) + 8+A_PtrSize)
        this._ParseScriptText(Code, 0x20, pvar)  ; SCRIPTTEXT_ISEXPRESSION := 0x20
        return arr[0]
    }
    
    Exec(Code)
    {
        this._ParseScriptText(Code, 0x42, 0)  ; SCRIPTTEXT_ISVISIBLE := 2, SCRIPTTEXT_ISPERSISTENT := 0x40
        this._SetScriptState(2)  ; SCRIPTSTATE_CONNECTED := 2
    }
    
    AddObject(Name, DispObj, AddMembers := false)
    {
        static a, supports_dispatch ; Test for built-in IDispatch support.
            := a := ((a:=ComObjArray(0xC,1))[0]:=[42]) &amp;&amp; a[0][1]=42
        if IsObject(DispObj) &amp;&amp; !(supports_dispatch || ComObjType(DispObj))
            throw Exception(&quot;Adding a non-COM object requires AutoHotkey v1.1.17+&quot;, -1)
        this._objects[Name] := DispObj
        this._AddNamedItem(Name, AddMembers ? 8 : 2)  ; SCRIPTITEM_ISVISIBLE := 2, SCRIPTITEM_GLOBALMEMBERS := 8
    }
    
    _GetObjectUnk(Name)
    {
        return !IsObject(dsp := this._objects[Name]) ? dsp  ; Pointer
            : ComObjValue(dsp) ? ComObjValue(dsp)  ; ComObject
            : &amp;dsp  ; AutoHotkey object
    }
    
    class _base
    {
        __Call(Method, Params*)
        {
            if ObjHasKey(this, &quot;_dsp&quot;)
                try
                    return (this._dsp)[Method](Params*)
                catch e
                    throw Exception(e.Message, -1, e.Extra)
        }
        
        __Get(Property, Params*)
        {
            if ObjHasKey(this, &quot;_dsp&quot;)
                try
                    return (this._dsp)[Property, Params*]
                catch e
                    throw Exception(e.Message, -1, e.Extra)
        }
        
        __Set(Property, Params*)
        {
            if ObjHasKey(this, &quot;_dsp&quot;)
            {
                Value := Params.Pop()
                try
                    return (this._dsp)[Property, Params*] := Value
                catch e
                    throw Exception(e.Message, -1, e.Extra)
            }
        }
    }
    
    _SetScriptSite(Site)
    {
        hr := DllCall(NumGet(NumGet((p:=this._script)+0)+3*A_PtrSize), &quot;ptr&quot;, p, &quot;ptr&quot;, Site)
        if (hr &lt; 0)
            this._HRFail(hr, &quot;IActiveScript::SetScriptSite&quot;)
    }
    
    _SetScriptState(State)
    {
        hr := DllCall(NumGet(NumGet((p:=this._script)+0)+5*A_PtrSize), &quot;ptr&quot;, p, &quot;int&quot;, State)
        if (hr &lt; 0)
            this._HRFail(hr, &quot;IActiveScript::SetScriptState&quot;)
    }
    
    _AddNamedItem(Name, Flags)
    {
        hr := DllCall(NumGet(NumGet((p:=this._script)+0)+8*A_PtrSize), &quot;ptr&quot;, p, &quot;wstr&quot;, Name, &quot;uint&quot;, Flags)
        if (hr &lt; 0)
            this._HRFail(hr, &quot;IActiveScript::AddNamedItem&quot;)
    }
    
    _GetScriptDispatch()
    {
        hr := DllCall(NumGet(NumGet((p:=this._script)+0)+10*A_PtrSize), &quot;ptr&quot;, p, &quot;ptr&quot;, 0, &quot;ptr*&quot;, pdsp)
        if (hr &lt; 0)
            this._HRFail(hr, &quot;IActiveScript::GetScriptDispatch&quot;)
        return ComObject(9, pdsp, 1)
    }
    
    _InitNew()
    {
        hr := DllCall(NumGet(NumGet((p:=this._scriptParse)+0)+3*A_PtrSize), &quot;ptr&quot;, p)
        if (hr &lt; 0)
            this._HRFail(hr, &quot;IActiveScriptParse::InitNew&quot;)
    }
    
    _ParseScriptText(Code, Flags, pvarResult)
    {
        VarSetCapacity(excp, 8 * A_PtrSize, 0)
        hr := DllCall(NumGet(NumGet((p:=this._scriptParse)+0)+5*A_PtrSize), &quot;ptr&quot;, p
            , &quot;wstr&quot;, Code, &quot;ptr&quot;, 0, &quot;ptr&quot;, 0, &quot;ptr&quot;, 0, &quot;uptr&quot;, 0, &quot;uint&quot;, 1
            , &quot;uint&quot;, Flags, &quot;ptr&quot;, pvarResult, &quot;ptr&quot;, 0)
        if (hr &lt; 0)
            this._HRFail(hr, &quot;IActiveScriptParse::ParseScriptText&quot;)
    }
    
    _HRFail(hr, what)
    {
        if e := this.Error
        {
            this.Error := &quot;&quot;
            throw Exception(&quot;`nError code:`t&quot; this._HRFormat(e.HRESULT)
                . &quot;`nSource:`t`t&quot; e.Source &quot;`nDescription:`t&quot; e.Description
                . &quot;`nLine:`t`t&quot; e.Line &quot;`nColumn:`t`t&quot; e.Column
                . &quot;`nLine text:`t`t&quot; e.LineText, -3)
        }
        throw Exception(what &quot; failed with code &quot; this._HRFormat(hr), -2)
    }
    
    _HRFormat(hr)
    {
        return Format(&quot;0x{1:X}&quot;, hr &amp; 0xFFFFFFFF)
    }
    
    _OnScriptError(err) ; IActiveScriptError err
    {
        VarSetCapacity(excp, 8 * A_PtrSize, 0)
        DllCall(NumGet(NumGet(err+0)+3*A_PtrSize), &quot;ptr&quot;, err, &quot;ptr&quot;, &amp;excp) ; GetExceptionInfo
        DllCall(NumGet(NumGet(err+0)+4*A_PtrSize), &quot;ptr&quot;, err, &quot;uint*&quot;, srcctx, &quot;uint*&quot;, srcline, &quot;int*&quot;, srccol) ; GetSourcePosition
        DllCall(NumGet(NumGet(err+0)+5*A_PtrSize), &quot;ptr&quot;, err, &quot;ptr*&quot;, pbstrcode) ; GetSourceLineText
        code := StrGet(pbstrcode, &quot;UTF-16&quot;), DllCall(&quot;OleAut32\SysFreeString&quot;, &quot;ptr&quot;, pbstrcode)
        if fn := NumGet(excp, 6 * A_PtrSize) ; pfnDeferredFillIn
            DllCall(fn, &quot;ptr&quot;, &amp;excp)
        wcode := NumGet(excp, 0, &quot;ushort&quot;)
        hr := wcode ? 0x80040200 + wcode : NumGet(excp, 7 * A_PtrSize, &quot;uint&quot;)
        this.Error := {HRESULT: hr, Line: srcline, Column: srccol, LineText: code}
        static Infos := &quot;Source,Description,HelpFile&quot;
        Loop Parse, % Infos, `,
            if pbstr := NumGet(excp, A_Index * A_PtrSize)
                this.Error[A_LoopField] := StrGet(pbstr, &quot;UTF-16&quot;), DllCall(&quot;OleAut32\SysFreeString&quot;, &quot;ptr&quot;, pbstr)
        return 0x80004001 ; E_NOTIMPL (let Exec/Eval get a fail result)
    }
    
    __Delete()
    {
        if this._script
        {
            DllCall(NumGet(NumGet((p:=this._script)+0)+7*A_PtrSize), &quot;ptr&quot;, p)  ; Close
            ObjRelease(this._script)
        }
        if this._scriptParse
            ObjRelease(this._scriptParse)
    }
    
    static IID := &quot;{BB1A2AE1-A4F9-11cf-8F20-00805F2CD064}&quot;
    static IID_Parse := A_PtrSize=8 ? &quot;{C7EF7658-E1EE-480E-97EA-D52CB4D76D17}&quot; : &quot;{BB1A2AE2-A4F9-11cf-8F20-00805F2CD064}&quot;
}

class ActiveScriptSite
{
    __New(Script)
    {
        ObjSetCapacity(this, &quot;_site&quot;, 3 * A_PtrSize)
        NumPut(&amp;Script
        , NumPut(ActiveScriptSite._vftable(&quot;_vft_w&quot;, &quot;31122&quot;, 0x100)
        , NumPut(ActiveScriptSite._vftable(&quot;_vft&quot;, &quot;31125232211&quot;, 0)
            , this.ptr := ObjGetAddress(this, &quot;_site&quot;))))
    }
    
    _vftable(Name, PrmCounts, EIBase)
    {
        if p := ObjGetAddress(this, Name)
            return p
        ObjSetCapacity(this, Name, StrLen(PrmCounts) * A_PtrSize)
        p := ObjGetAddress(this, Name)
        Loop Parse, % PrmCounts
        {
            cb := RegisterCallback(&quot;_ActiveScriptSite&quot;, &quot;F&quot;, A_LoopField, A_Index + EIBase)
            NumPut(cb, p + (A_Index-1) * A_PtrSize)
        }
        return p
    }
}

_ActiveScriptSite(this, a1:=0, a2:=0, a3:=0, a4:=0, a5:=0)
{
    Method := A_EventInfo &amp; 0xFF
    if A_EventInfo &gt;= 0x100  ; IActiveScriptSiteWindow
    {
        if Method = 4  ; GetWindow
        {
            NumPut(0, a1+0) ; *phwnd := 0
            return 0 ; S_OK
        }
        if Method = 5  ; EnableModeless
        {
            return 0 ; S_OK
        }
        this -= A_PtrSize     ; Cast to IActiveScriptSite
    }
    ;else: IActiveScriptSite
    if Method = 1  ; QueryInterface
    {
        iid := _AS_GUIDToString(a1)
        if (iid = &quot;{00000000-0000-0000-C000-000000000046}&quot;  ; IUnknown
         || iid = &quot;{DB01A1E3-A42B-11cf-8F20-00805F2CD064}&quot;) ; IActiveScriptSite
        {
            NumPut(this, a2+0)
            return 0 ; S_OK
        }
        if (iid = &quot;{D10F6761-83E9-11cf-8F20-00805F2CD064}&quot;) ; IActiveScriptSiteWindow
        {
            NumPut(this + A_PtrSize, a2+0)
            return 0 ; S_OK
        }
        NumPut(0, a2+0)
        return 0x80004002 ; E_NOINTERFACE
    }
    if Method = 5  ; GetItemInfo
    {
        a1 := StrGet(a1, &quot;UTF-16&quot;)
        , (a3 &amp;&amp; NumPut(0, a3+0))  ; *ppiunkItem := NULL
        , (a4 &amp;&amp; NumPut(0, a4+0))  ; *ppti := NULL
        if (a2 &amp; 1) ; SCRIPTINFO_IUNKNOWN
        {
            if !(unk := Object(NumGet(this + A_PtrSize*2))._GetObjectUnk(a1))
                return 0x8002802B ; TYPE_E_ELEMENTNOTFOUND
            ObjAddRef(unk), NumPut(unk, a3+0)
        }
        return 0 ; S_OK
    }
    if Method = 9  ; OnScriptError
        return Object(NumGet(this + A_PtrSize*2))._OnScriptError(a1)
    
    ; AddRef and Release don&#039;t do anything because we want to avoid circular references.
    ; The site and IActiveScript are both released when the AHK script releases its last
    ; reference to the ActiveScript object.
    
    ; All of the other methods don&#039;t require implementations.
    return 0x80004001 ; E_NOTIMPL
}

_AS_GUIDToString(pGUID)
{
    VarSetCapacity(String, 38*2)
    DllCall(&quot;ole32\StringFromGUID2&quot;, &quot;ptr&quot;, pGUID, &quot;str&quot;, String, &quot;int&quot;, 39)
    return String
}


/*
 *  JsRT for AutoHotkey v1.1
 *
 *  Utilizes the JavaScript engine that comes with IE11.
 *
 *  License: Use, modify and redistribute without limitation, but at your own risk.
 */
class JsRT extends ActiveScript._base
{
    __New()
    {
        throw Exception(&quot;This class is abstract. Use JsRT.IE or JSRT.Edge instead.&quot;, -1)
    }
    
    class IE extends JsRT
    {
        __New()
        {
            if !this._hmod := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, &quot;jscript9&quot;, &quot;ptr&quot;)
                throw Exception(&quot;Failed to load jscript9.dll&quot;, -1)
            if DllCall(&quot;jscript9\JsCreateRuntime&quot;, &quot;int&quot;, 0, &quot;int&quot;, -1
                , &quot;ptr&quot;, 0, &quot;ptr*&quot;, runtime) != 0
                throw Exception(&quot;Failed to initialize JsRT&quot;, -1)
            DllCall(&quot;jscript9\JsCreateContext&quot;, &quot;ptr&quot;, runtime, &quot;ptr&quot;, 0, &quot;ptr*&quot;, context)
            this._Initialize(&quot;jscript9&quot;, runtime, context)
        }
    }
    
    class Edge extends JsRT
    {
        __New()
        {
            if !this._hmod := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, &quot;chakra&quot;, &quot;ptr&quot;)
                throw Exception(&quot;Failed to load chakra.dll&quot;, -1)
            if DllCall(&quot;chakra\JsCreateRuntime&quot;, &quot;int&quot;, 0
                , &quot;ptr&quot;, 0, &quot;ptr*&quot;, runtime) != 0
                throw Exception(&quot;Failed to initialize JsRT&quot;, -1)
            DllCall(&quot;chakra\JsCreateContext&quot;, &quot;ptr&quot;, runtime, &quot;ptr*&quot;, context)
            this._Initialize(&quot;chakra&quot;, runtime, context)
        }
        
        ProjectWinRTNamespace(namespace)
        {
            return DllCall(&quot;chakra\JsProjectWinRTNamespace&quot;, &quot;wstr&quot;, namespace)
        }
    }
    
    _Initialize(dll, runtime, context)
    {
        this._dll := dll
        this._runtime := runtime
        this._context := context
        DllCall(dll &quot;\JsSetCurrentContext&quot;, &quot;ptr&quot;, context)
        DllCall(dll &quot;\JsGetGlobalObject&quot;, &quot;ptr*&quot;, globalObject)
        this._dsp := this._JsToVt(globalObject)
    }
    
    __Delete()
    {
        this._dsp := &quot;&quot;
        if dll := this._dll
        {
            DllCall(dll &quot;\JsSetCurrentContext&quot;, &quot;ptr&quot;, 0)
            DllCall(dll &quot;\JsDisposeRuntime&quot;, &quot;ptr&quot;, this._runtime)
        }
        DllCall(&quot;FreeLibrary&quot;, &quot;ptr&quot;, this._hmod)
    }
    
    _JsToVt(valref)
    {
        VarSetCapacity(variant, 24, 0)
        DllCall(this._dll &quot;\JsValueToVariant&quot;, &quot;ptr&quot;, valref, &quot;ptr&quot;, &amp;variant)
        ref := ComObject(0x400C, &amp;variant), val := ref[], ref[] := 0
        return val
    }
    
    _ToJs(val)
    {
        VarSetCapacity(variant, 24, 0)
        ref := ComObject(0x400C, &amp;variant) ; VT_BYREF|VT_VARIANT
        ref[] := val
        DllCall(this._dll &quot;\JsVariantToValue&quot;, &quot;ptr&quot;, &amp;variant, &quot;ptr*&quot;, valref)
        ref[] := 0
        return valref
    }
    
    _JsEval(code)
    {
        e := DllCall(this._dll &quot;\JsRunScript&quot;, &quot;wstr&quot;, code, &quot;uptr&quot;, 0, &quot;wstr&quot;, &quot;source.js&quot;
            , &quot;ptr*&quot;, result)
        if e
        {
            if DllCall(this._dll &quot;\JsGetAndClearException&quot;, &quot;ptr*&quot;, excp) = 0
                throw this._JsToVt(excp)
            throw Exception(&quot;JsRT error&quot;, -2, format(&quot;0x{:X}&quot;, e))
        }
        return result
    }
    
    Exec(code)
    {
        this._JsEval(code)
    }
    
    Eval(code)
    {
        return this._JsToVt(this._JsEval(code))
    }
    
    AddObject(name, obj, addMembers := false)
    {
        if addMembers
            throw Exception(&quot;AddMembers=true is not supported&quot;, -1)
        this._dsp[name] := obj
    }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Malcev]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=26930</uri>
			</author>
			<updated>2024-09-21T18:31:46Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=161630#p161630</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Решить пример, записанный строкой?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=161622#p161622" />
			<content type="html"><![CDATA[<p><strong>teadrinker</strong> Ага, ясно, спасибо, пойду попробую.</p>]]></content>
			<author>
				<name><![CDATA[Gh0sTG0]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=39523</uri>
			</author>
			<updated>2024-09-21T07:27:54Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=161622#p161622</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Решить пример, записанный строкой?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=161621#p161621" />
			<content type="html"><![CDATA[<p>Единственная возможность — запустить другой процесс, который будет воспринимать выражение, как часть кода. Пара вариантов:<br /></p><div class="codebox"><pre><code>expr := &quot;1234+852-12&quot;
MsgBox % Math1(expr) . &quot;`n&quot; . Math2(expr)

Math1(expr, ahkPath := &quot;&quot;) {
    script .= &quot;#NoTrayIcon`nFileAppend, % &quot; . expr . &quot;, *&quot;
    (ahkPath = &quot;&quot; &amp;&amp; ahkPath := A_AHKPath)
    Shell := ComObjCreate(&quot;WScript.Shell&quot;)
    Exec := Shell.Exec(ahkPath . &quot; *&quot;)
    Exec.StdIn.Write(script)
    Exec.StdIn.Close()
    return Exec.StdOut.ReadAll()
}

Math2(expr) {
    static Doc, JS
    if !Doc {
       Doc := ComObjCreate(&quot;htmlfile&quot;)
       Doc.write(&quot;&lt;meta http-equiv=&quot;&quot;X-UA-Compatible&quot;&quot; content=&quot;&quot;IE=9&quot;&quot;&gt;&quot;)
       JS := Doc.parentWindow
       ( Doc.documentMode &lt; 9 &amp;&amp; JS.execScript() )
    }
    return JS.eval(&quot;(&quot; . expr . &quot;)&quot;)
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2024-09-20T23:04:13Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=161621#p161621</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[AHK: Решить пример, записанный строкой?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=161620#p161620" />
			<content type="html"><![CDATA[<p>Добрый вечер.<br />Возник такой интерес: существует ли какой то готовый модуль, который позволит забить пример строкой, условно:<br /></p><div class="codebox"><pre><code>1234+852-12</code></pre></div><p>И получить ответ 2074 без того, чтобы как то его разбирать? Действия предполагаются простейшие, никаких синусов и прочего, простое вычисление.</p>]]></content>
			<author>
				<name><![CDATA[Gh0sTG0]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=39523</uri>
			</author>
			<updated>2024-09-20T18:06:27Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=161620#p161620</id>
		</entry>
</feed>
