[Request] Module Export

Propose new features and changes
User avatar
Chunjee
Posts: 330
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

[Request] Module Export

17 Oct 2018, 08:29

Hello,

I am officially requesting Moduled Exports in the likeness of Node.js: https://nodejs.org/api/modules.html
It would be nice to be able to use external functions without following the naming convention the function creator chose.

Psuedocode:

Code: Select all

Class exampleClass{
	
	__New() {
	}
	

	exampleMethod(parameter) {
		;do something
	}
	
Export(exampleClass)

Code: Select all

exampleFunction(parameter1, parameter2) {
	;do something
}

Export(exampleFunction)

Code: Select all

Import(module.ahk)
myclassinstance := new exampleClass()

mynamedfunction := Import(function.ahk)
mynamedfunction("foo","bar")
#import %filename% as %varname% also a good suggestion.



As a note, its easy in javascript as everything including functions can be saved as objects and transported elsewhere:

Code: Select all

function myCoolFunction() {
// do something
}
module.exports = {
	myCoolFunction: myCoolFunction
};

Code: Select all

var myCoolFunctionInstance = require('myCoolFunction');
// searches recursively in parent directory for lodash library and imports as user specified object.
myCoolFunctionInstance.doSomething()

In addition: I have been working around the outdated way AHK sources code from other files, but increasingly I am coming up against an issue where the same function cannot be defined twice:
Known limitation: Currently, the name of each subroutine (label) must be unique among those of the entire script. The program will notify you upon launch if there are duplicate labels.
This turns into an issue when for example two libraries share a dependency on a third library.


Notes and links:
https://autohotkey.com/docs/Functions.htm#lib
Last edited by Chunjee on 14 Nov 2018, 16:14, edited 10 times in total.
User avatar
nnnik
Posts: 4384
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Module Export

17 Oct 2018, 08:37

#import %filename% as %varname% would be my prefered syntax for this.
All files loaded like this receive their own scope and settings.
They also receive their own A_Variables and functions.
In this syntax the person creating the library does not need to care about the way it is imported and we can keep the current libraries unchanged.
Recommends AHK Studio
User avatar
Chunjee
Posts: 330
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: [Request] Module Export

18 Dec 2018, 15:23

https://autohotkey.com/docs/commands/_Include.htm
#IncludeAgain allows multiple inclusions of the same file, while being the same as #Include in all other respects.
I'll see if this can be leveraged in any way.
User avatar
kczx3
Posts: 999
Joined: 06 Oct 2015, 21:39

Re: [Request] Module Export

18 Dec 2018, 16:48

Can you explain how that may be helpful?
User avatar
Chunjee
Posts: 330
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: [Request] Module Export

19 Dec 2018, 15:25

For some reason, I thought it would allow multiple a the same function to be defined more than once but I was wrong.
coffee
Posts: 114
Joined: 01 Apr 2017, 07:55

Re: [Request] Module Export

15 Feb 2019, 03:01

This *may* be emulated using AutoHotkey_H (latest version, v2) if you want to experiment. You can try asking HotkeyIt for some help with this. He can probably iron out any quirks to make it work 'neatly', if you propose something.

Raw proof of concept:
For a file called "namedModule.ahk"

Code: Select all

exports.world := func("world")
exports.xvar := "holy canoli"
world()
{
	msgbox("inside")
}
It could be "required" as

Code: Select all

mymod := require("namedModule")

mymod.world()
msgbox(mymod.xvar)


;/////////////////// REQUIRE
require(mod)
{
	static CRLF := "`r`n"
	local obj := CriticalObject()
	local script := FileRead(mod . ".ahk")
	
	script := (
		"#persistent" CRLF
		"exports := CriticalObject(" (&obj) ")" CRLF
		script
	)
	ThreadObj(script).ExitApp()
	return obj
}
That example is working in my setup, albeit finicky.
I'm unaware of memory leaks, or performance ramifications, since I literally just sprung this up and I don't really use the multithreading features of autohotkey_h that much.
User avatar
kczx3
Posts: 999
Joined: 06 Oct 2015, 21:39

Re: [Request] Module Export

15 Feb 2019, 10:56

What do you mean by finicky?
coffee
Posts: 114
Joined: 01 Apr 2017, 07:55

Re: [Request] Module Export

16 Feb 2019, 00:29

kczx3 wrote:
15 Feb 2019, 10:56
What do you mean by finicky?
If you look at it wrong it throws an access violation.
Doing msgbox(type(mymod.world)) above the mymod.world() call in previous post either hangs, throws access violation or works, or all three at the same time ¯\_(ツ)_/¯

Using ahkthread seems more consistent, though I don't know if resources are released automatically. Doing .ahkterminate() throws access violation. Maybe someone who uses autohotkey_h a lot and its variety of ways of creating threads can get it to work for their use.

Code: Select all

world()
{
	global glob
	msgbox(glob " | " superglob)
}

glob := "global"
global superglob := "superglobal"

class test {
	method1()
	{
		msgbox("method1")
	}
	
}

exports.world := func("world")
exports.xvar := "holy canoli"
exports.classify := test

Code: Select all

mymod := require("namedmodule")

; msgbox(type(mymod.world))
mymod.world()
mymod.classify.method1()
msgbox(mymod.xvar)


;/////////////////// REQUIRE
require(mod)
{
	static CRLF := "`r`n"
	local obj := CriticalObject()
	
	script := (
		"#persistent" CRLF
		"exports := CriticalObject(" (&obj) ")" CRLF
		"#include " mod ".ahk"
	)
	; ThreadObj(script).ExitApp()
	AhkThread(script)
	return obj
}

User avatar
Chunjee
Posts: 330
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: [Request] Module Export

17 Feb 2019, 10:59

Thanks I'll take a look at that.
User avatar
Chunjee
Posts: 330
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: [Request] Module Export

20 Jan 2020, 10:25

This request is still open. I guess #Include just works on the global namespace so there's really no clean way to build classes that depend on classes or functions because any change at the global namespace will screw them up.
swagfag
Posts: 3273
Joined: 11 Jan 2017, 17:59

Re: [Request] Module Export

20 Jan 2020, 11:43

i dont think this is gonna be needed if namespaces were to be introduced
there was a fully working PR by @Helgef that just festers nowadays..
User avatar
nnnik
Posts: 4384
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [Request] Module Export

21 Jan 2020, 08:01

I think Helgefs concept of namespaces was overly bloated and complex.
Though it definitively would work.
Recommends AHK Studio
User avatar
kczx3
Posts: 999
Joined: 06 Oct 2015, 21:39

Re: [Request] Module Export

21 Jan 2020, 08:31

nnnik wrote:
21 Jan 2020, 08:01
I think Helgefs concept of namespaces was overly bloated and complex.
Though it definitively would work.
It is certainly a complicated issue to solve due to the very global nature of many of AHKs settings
Helgef
Posts: 4106
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Request] Module Export

21 Jan 2020, 10:49

Bloated and complex, perhaps in regards to the implementation, but usage is very simple and offers powerful possibilities, imo. However, I did start to rewrite it just a few days ago, I will make the implementation simpler (I hope), but usage will be much more limited. Whether I endure the project or not remains to be seen though ;) .

Cheers.
User avatar
Chunjee
Posts: 330
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: [Request] Module Export

21 Jan 2020, 11:33

Helgef wrote:
21 Jan 2020, 10:49
Bloated and complex, perhaps in regards to the implementation, but usage is very simple and offers powerful possibilities, imo. However, I did start to rewrite it just a few days ago, I will make the implementation simpler (I hope), but usage will be much more limited.
Where do I find this?
Helgef
Posts: 4106
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Request] Module Export

21 Jan 2020, 12:21

It is for v2, iirc a103. See :arrow: #142, :arrow: documentation and executables.
Cheers.
HotKeyIt
Posts: 1987
Joined: 29 Sep 2013, 18:35
Contact:

Re: [Request] Module Export

21 Jan 2020, 16:42

Here is an example for AHK_H v2, CriticalObject is only required if you want to use it in multi-threading environment:

Code: Select all

module:="
(
world(this){
	global glob
	msgbox(glob " | " superglob)
}
glob := "global"
global superglob := "superglobal"
class test {
	method1(){
		msgbox("method1")
	}	
}
exports.DefineMethod("world",func("world"))
exports.xvar := "holy canoli"
exports.classify := test.new()
)"

mymod := require(module)

mymod.world()
mymod.classify.method1()
msgbox(mymod.xvar)

require(module){
	thread:=ExeThread("#persistent`nexports := CriticalObject(" (&obj := CriticalObject()) ")`n" module "`nA_IMPORT_FINALIZED:=1") ; to import from string
	; thread:=ExeThread("#persistent`nexports := CriticalObject(" (&obj := {}) ")`n#import " module ".ahk`nA_IMPORT_FINALIZED:=1") ; to import a file
    While !thread.ahkgetvar("A_IMPORT_FINALIZED")
      Sleep 10
	return obj
}

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 4 guests