Skip to content

Conversation

@Xmader
Copy link
Member

@Xmader Xmader commented Jun 29, 2023

No description provided.

@Xmader Xmader requested a review from wesgarland June 29, 2023 04:52
def require(moduleIdentifier: str):
# Retrieve the caller’s filename from the call stack
filename = inspect.stack()[1].filename
return createRequire(filename)(moduleIdentifier)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has the side effect of creating a new, unique, require function for the current module each time it is invoked, because createRequire instantiates the CtxModule constructor, creating a new "module" for the require to live in. That will also cause us to subtly violate the CommonJS principle that each module is a singleton.

I think we avoid all of the possible subtle gotchas and possibly perform a little faster if we cache the return value of createRequire in a dict or something, always using the same require for the same filename.

This will be especially important once we support require.cache, require.path, etc, from Python.

In the future, the best way to do that cache lookup might be to look at require.cache, but PythonMonkey does not currently support function properties, so we can't do that today.

The stack filename is `<stdin>` for the REPL, which is not a valid path
@Xmader Xmader requested a review from wesgarland July 4, 2023 18:12
Base automatically changed from wes/module-system to main July 5, 2023 02:54
@Xmader Xmader merged commit f27b51c into main Jul 10, 2023
@Xmader Xmader deleted the Xmader/feat/pm.require branch July 10, 2023 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants