Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are modules serializable? #46

Open
sancarn opened this issue Mar 2, 2021 · 4 comments
Open

Are modules serializable? #46

sancarn opened this issue Mar 2, 2021 · 4 comments
Labels
host environment Questions to be answered in each host environment

Comments

@sancarn
Copy link

sancarn commented Mar 2, 2021

I am very excited about this idea, as it finally gives us syntax sugar for a scopeless blocks! Are modules serializable?

Scenarios like node's @jxa library should be considered in this scenario. JXA has global Application and $ objects which already pose typescript issues ofc, but more to the point it's difficult to see from libraries such as @jxa, that variables won't be in scope. E.G

import { run } from "@jxa/run";
let someVar = 1
export const currentUserName = () => {
    // This callback function is run as JXA
    return run(() => {
        return someVar;
    });
};
currentUserName() //returns undefined

At least with modules it will be very clear that this is a new and seperate scope.

export const currentUserName = new JXAFunction(
    module {
        const sys = Application("System Events");
        return sys.currentUser().name();
    }
)
currentUserName() //returns <current user name>

Features like this will only be doable if module are serializable, however.

@littledan
Copy link
Member

Sorry, what do you mean by serializable exactly? Module blocks support .toString() and postMessage.

@sancarn
Copy link
Author

sancarn commented Mar 2, 2021

I guess in an ideal world it'd be nice if you could just obtain the body of the code block. But even getting module {...} via toString() would be sufficient.

@Jack-Works
Copy link
Member

I guess the answer is yes, it may pass the structured clone algorithms but that algorithm is not defined in the JS.

@Jamesernator
Copy link

Just as a note, the code provided:

export const currentUserName = new JXAFunction(
    module {
        const sys = Application("System Events");
        return sys.currentUser().name();
    }
)
currentUserName() //returns <current user name>

Won't work because the code inside module { still needs to be a syntatically valid module, and as return is not allowed in the top level of a module, this will be a SyntaxError.

You can just export a function though, and import and call that function instead:

module {
    export default function() {
        const sys = Application("System Events");
        return sys.currentUser().name();
    }
}

@sancarn sancarn changed the title Are modules be serializable? Are modules serializable? Apr 14, 2021
@nicolo-ribaudo nicolo-ribaudo added the host environment Questions to be answered in each host environment label Oct 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
host environment Questions to be answered in each host environment
Projects
None yet
Development

No branches or pull requests

5 participants