Page 1 of 1

eval (using JS/COM)

Posted: 28 Mar 2016, 19:14
by tidbit
eval
Version 1.1 (Tuesday, March 29, 2016)

Description:
Need to do math on a piece of text like "5+5-74*pi"? AHK doesn't allow that! But now you can.
All the other eval things on the forums are massive. AHK now has COM. COM can do IE/JS stuff. JS supports eval(). Tap into JS with AHK and you got a nice simple powerful short eval function.

EXAMPLES:

Code: Select all

; example 1
var:=eval("pi*e*4")
msgbox %var%

; example 2
msgbox, % eval("5+5")

; example 3
if (eval("5+3")=132)
	msgbox, your math is wrong
else
	msgbox, 5+3 does not equal 132!

; example 4
var=10
msgbox % eval("%var%+10+" var)
FUNCTION/DOWNLOAD:

Code: Select all

; Name: eval
; Version 1.1 (Tuesday, March 29, 2016)
; Created: Monday, March 28, 2016
; Author: tidbit
; Credit: TLM
; Description: Uses Javascript/COM to evaluate stringed math expressions.
; Supported constants and functions: http://www.w3schools.com/jsref/jsref_obj_math.asp
; Example: msgbox, % eval("5*pi+(7*sqrt(27))/2")

eval(exp)
{
	transform, exp, deref, %exp%
	; make everything lowercase, set constants to uppercase
	exp:=format("{:l}", exp) 
	exp:=regExReplace(exp, "i)(E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2)", "$U1")
	exp:=regExReplace(exp, "i)(abs|acos|asin|atan|atan2|ceil|cos|exp|floor"
	. "|log|max|min|pow|random|round|sin|sqrt|tan"
	. "|E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2)", "Math.$1")
    obj:=ComObjCreate("HTMLfile")
    obj.write("<body><script>document.body.innerText=eval('" exp "');</script>")
    return inStr(cabbage:=obj.body.innerText, "body") ? "ERROR" : cabbage
}
History:
Spoiler

Re: eval (using JS/COM)

Posted: 28 Mar 2016, 20:49
by wo52616111
AWESOME!! I am a front-end coincidentally, I don't know that AHK can run js before. It's that mean I can do any thing in AHK that js can do?!

Re: eval (using JS/COM)

Posted: 28 Mar 2016, 20:59
by tidbit
Um, kinda? not really an expert, TLM did that part (though, I modified it a tiny bit).
as far as I know, it can do whatever IE* can do. AHK also has a built-in activex control (most often used as a webbrowser* control) which is powered by IE*. So if IE* can do it and you know how to pull info from IE* with COM and/or event listener stuff, you can do it in AHK.

* IE/activex by default uses an older IE version, I think IE7. There are ways to upgrade the engine used: https://autohotkey.com/board/topic/9366 ... er-engine/

Re: eval (using JS/COM)

Posted: 28 Mar 2016, 22:26
by wo52616111
tidbit wrote:* IE/activex by default uses an older IE version, I think IE7. There are ways to upgrade the engine used: https://autohotkey.com/board/topic/9366 ... er-engine/

Code: Select all

obj:=ComObjCreate("HTMLfile")
obj.write("<body><script>document.body.innerText=eval('alert(navigator.userAgent)');</script></body>")
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; Win64; x64; Trident/7.0; .NET4.0C; .NET4.0E; InfoPath.3; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)
yes, you are right. And the script to upgrade the engine used is work fine. Maybe I can do some interesting things by using js I think. Thanks a lot XD.

Re: eval (using JS/COM)

Posted: 29 Mar 2016, 12:09
by tidbit
1.1 - added support for variables in %'s.

Code: Select all

; example 4
var=10
msgbox % eval("%var%+10+" var)

Re: eval (using JS/COM)

Posted: 29 Mar 2016, 20:18
by guest3456
nice work :)

Re: eval (using JS/COM)

Posted: 10 Apr 2016, 22:50
by JJohnston2
I use an adaptation of this code for doing native eval() calls.

I'm guessing the COM call to use IE/JS is slower but more convenient if you're familiar with the syntax.

Re: eval (using JS/COM)

Posted: 16 Apr 2016, 14:49
by tidbit
That would be one of the "All the other eval things on the forums are massive." I mentioned :) I used to use it all the time, great function. but it gets annoying having something so large with your code for a small feature.

also, the syntax for mine is the same, but it does have the options to use JS functions (if you know them). otherwise simply: "5+5" or "%var%*5" also works. Nothing complex.

As for speed, I have not tested. was just a 1-time thing tlm and I made, it worked great and is short so I shared. didn't bother about speed/better methods

Re: eval (using JS/COM)

Posted: 06 Aug 2017, 08:32
by wolf_II
After I butchered your nice function, I posted it again in a Ask for Help thread. https://autohotkey.com/boards/viewtopic ... 21#p139121
Maybe that is bad style, I really don't know, and I apologize.

More importantly though: In that same thread, forum user jeeswg posted he found a bug and also a fix.
Please Try this:

Code: Select all

msgbox % eval("exp(1)") ; expect to see 2.718..

; Name: eval
; Version 1.1 (Tuesday, March 29, 2016)
; Created: Monday, March 28, 2016
; Author: tidbit
; Credit: TLM
; Description: Uses Javascript/COM to evaluate stringed math expressions.
; Supported constants and functions: http://www.w3schools.com/jsref/jsref_obj_math.asp
; Example: msgbox, % eval("5*pi+(7*sqrt(27))/2")

eval(exp)
{
	transform, exp, deref, %exp%
	; make everything lowercase, set constants to uppercase
	exp:=format("{:l}", exp)
	exp:=regExReplace(exp, "i)(E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2)", "$U1")
	exp:=regExReplace(exp, "i)(abs|acos|asin|atan|atan2|ceil|cos|exp|floor"
	. "|log|max|min|pow|random|round|sin|sqrt|tan"
	. "|E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2)", "Math.$1")
    obj:=ComObjCreate("HTMLfile")
    obj.write("<body><script>document.body.innerText=eval('" exp "');</script>")
    return inStr(cabbage:=obj.body.innerText, "body") ? "ERROR" : cabbage
}
vs.

Code: Select all

msgbox % eval("exp(1)") ; expect to see 2.718..

; Name: eval
; Version 1.1 (Tuesday, March 29, 2016)
; Created: Monday, March 28, 2016
; Author: tidbit
; Credit: TLM
; Description: Uses Javascript/COM to evaluate stringed math expressions.
; Supported constants and functions: http://www.w3schools.com/jsref/jsref_obj_math.asp
; Example: msgbox, % eval("5*pi+(7*sqrt(27))/2")

eval(exp)
{
	transform, exp, deref, %exp%
	; make everything lowercase, set constants to uppercase
	exp:=format("{:l}", exp)
	exp:=regExReplace(exp, "i)(E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2)", "$U1")
	exp:=regExReplace(exp, "i)(abs|acos|asin|atan|atan2|ceil|cos|exp|floor"
	. "|log|max|min|pow|random|round|sin|sqrt|tan"
	. "|E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2)", "Math.$1")

    Exp := StrReplace(Exp, "Math.ceil", "Math.ceil")
    Exp := StrReplace(Exp, "Math.exp", "Math.exp")

    obj:=ComObjCreate("HTMLfile")
    obj.write("<body><script>document.body.innerText=eval('" exp "');</script>")
    return inStr(cabbage:=obj.body.innerText, "body") ? "ERROR" : cabbage
}

Re: eval (using JS/COM)

Posted: 27 May 2021, 07:49
by likethevegetable
Hopefully, someone sees this, it's been a while...

Do you have an idea how to expand this code to incorporate the math.evaluate() function in this JS library: https://mathjs.org/index.html ?