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
Unable to import modules #600
Comments
This thread #479 has some details closer to the end, which is a recent change. |
Ah you appear to be correct, module imports seem to have changed, so therefore is failure to RTFM and not a bug. Will close this to avoid any further confusion. |
I don't think the docs have been updated just yet but I could be wrong! |
Yeah the website docs appear out of date currently. |
This issue might be a bit relevant actually, I added a module resolve function static const char *wrenResolveModuleFn(WrenVM *vm, const char *importer, const char *name) {
int length = strlen(name);
char *newname = malloc(length + 2);
newname[0] = '!'; // This can be anything as long as there's at least one character
newname[1] = '\0';
strcat(newname, name);
return newname;
} Unless I add an extra character at the start of the module name (to make it unique) Wren refuses to load it. Is this intentional or a bug? |
I'd suggest the test folder would have all relevant cases showing the usage, and maybe @munificent is around to comment on the nature of it in master. |
All the import tests in Also the |
Always assume that pointers with address < 0xffff is a NULL pointer offset
at first read.
And there are not much special values in the VM, so I can say for sure it
is a bug/null pointer.
|
Huh, I just tested a bit more, and my 2nd latest comment seems invnalid now. This code static const char *moduleString = "\n"
"class TestClass {\n"
" static testFunction() {\n"
" System.print(\"Test function!\")\n"
" }\n"
"}\n"
"\n";
static const char *sourceString = "\n"
"import \"io\" for Stdin\n"
"import \"mytest\" for TestClass\n"
"TestClass.testFunction()"
"\n";
static const char *wrenResolveModuleFn(WrenVM *vm, const char *importer, const char *name) {
int length = strlen(name);
char *newname = malloc(length + 2);
newname[0] = '!'; // This can be anything as long as there's at least one character
newname[1] = '\0';
strcat(newname, name);
return newname;
}
static char *wrenLoadModuleFn(WrenVM *vm, const char *name) {
if(!strcmp(name, "!mytest")) { return strdup(moduleString); }
return strdup("");
} still does not work, it seems that as long as the loadModuleFn returns a non-empty string, Wren fails to import the module for the same reason ( |
Le dim. 9 sept. 2018 à 20:45, KaadmY <notifications@github.com> a écrit :
Huh, I just tested a bit more, and my 2nd latest comment seems invnalid
now.
This code
static const char *moduleString = "\n"
"class TestClass {\n"
" static testFunction() {\n"
" System.print(\"Test function!\")\n"
" }\n"
"}\n"
"\n";
static const char *sourceString = "\n"
"import \"io\" for Stdin\n"
"import \"mytest\" for TestClass\n"
"TestClass.testFunction()"
"\n";
static const char *wrenResolveModuleFn(WrenVM *vm, const char *importer, const char *name) {
int length = strlen(name);
char *newname = malloc(length + 2);
newname[0] = '!'; // This can be anything as long as there's at least one character
newname[1] = '\0';
strcat(newname, name);
return newname;
}
static char *wrenLoadModuleFn(WrenVM *vm, const char *name) {
if(!strcmp(name, "!mytest")) { return strdup(moduleString); }
Why the '!' here?
return strdup("");
Why returning "" and not NULL? I wonder if this has no side effects trying
to load core.
…
}
still does not work, it seems that as long as the loadModuleFn returns a
non-empty string, Wren fails to import the module for the same reason (Error
at 'TestClass': Module variable is already defined.)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#600 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AY7blZNUShbUT0Ddy3e9A3xb6dsEb2cXks5uZWG4gaJpZM4WgHJa>
.
|
Yeah sorry, after posting I realized that code had some problems with it The I'm not sure why I returned an empty string instead of NULL, I probably added that and forgot it was supposed to be NULL. If I return NULL instead of an empty string, the Updated code: // moduleString is unchanged
// sourceString is unchanged except the line `import "io" for Stdin` is removed
static const char *wrenResolveModuleFn(WrenVM *vm, const char *importer, const char *name) {
printf("Resolve: %s\n", name);
return name;
}
static char *wrenLoadModuleFn(WrenVM *vm, const char *name) {
printf("Load: %s\n", name);
if(!strcmp(name, "mytest")) { return strdup(moduleString); }
return NULL;
} And upon running I still get this:
|
Related to the io part:
You can of course bring over the bindings from the cli etc if you wanted them, though. It'd be a bit of extra work, depending on your goals! |
Ah okay, so the |
So by changing this line in my main source file from wrenInterpret(vm, NULL, sourceString); to wrenInterpret(vm, "main", sourceString); all the import issues seem to no longer exist. I just checked the existing tests and this seems to be the proper method, and I was using wrenInterpret incorrectly. |
glad you figured that out, we should probably add some assertions or validations about what that parameter being null means. |
I'm using the latest Git version of Wren (744147f), and when I try to import a custom module Wren fails with the message
Error at 'TestClass': Module variable is already defined.
I can reliably reproduce with this code:
The line
import "io" for Stdin
does not fail, so this appears to be an issue related to custom modules.A previous version (at least a few weeks ago, probably 1-2 months) did not have this issue, so this seems to be a fairly recent regression.
Any help would be appreciated.
The text was updated successfully, but these errors were encountered: