eval (using JS/COM)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
tidbit
Posts: 1280
Joined: 29 Sep 2013, 17:15
Location: USA

eval (using JS/COM)

28 Mar 2016, 19:14

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
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
wo52616111
Posts: 47
Joined: 21 Aug 2014, 04:46

Re: eval (using JS/COM)

28 Mar 2016, 20:49

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?!
User avatar
tidbit
Posts: 1280
Joined: 29 Sep 2013, 17:15
Location: USA

Re: eval (using JS/COM)

28 Mar 2016, 20:59

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/
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
wo52616111
Posts: 47
Joined: 21 Aug 2014, 04:46

Re: eval (using JS/COM)

28 Mar 2016, 22:26

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.
User avatar
tidbit
Posts: 1280
Joined: 29 Sep 2013, 17:15
Location: USA

Re: eval (using JS/COM)

29 Mar 2016, 12:09

1.1 - added support for variables in %'s.

Code: Select all

; example 4
var=10
msgbox % eval("%var%+10+" var)
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
guest3456
Posts: 3477
Joined: 09 Oct 2013, 10:31

Re: eval (using JS/COM)

29 Mar 2016, 20:18

nice work :)

JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: eval (using JS/COM)

10 Apr 2016, 22:50

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.
User avatar
tidbit
Posts: 1280
Joined: 29 Sep 2013, 17:15
Location: USA

Re: eval (using JS/COM)

16 Apr 2016, 14:49

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
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: eval (using JS/COM)

06 Aug 2017, 08:32

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
}
likethevegetable
Posts: 85
Joined: 05 May 2021, 08:54

Re: eval (using JS/COM)

27 May 2021, 07:49

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 ?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: submeg and 132 guests