-
Notifications
You must be signed in to change notification settings - Fork 181
Win32 test std #1090
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
base: master
Are you sure you want to change the base?
Win32 test std #1090
Changes from 9 commits
6ee553f
f323678
7774b71
ba9e4c4
3d43bdb
60c56b1
a5395d3
e7e1a1f
18baa91
11e0daa
e402f19
e91b646
330df2a
7b68bff
4c04f21
f29e7b5
8d8b524
ec31538
68ec872
8988eca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1034,6 +1034,15 @@ typedef struct { | |
bool is_popen; | ||
} JSSTDFile; | ||
|
||
#if defined(__MINGW32__) || defined(__MINGW64__) | ||
typedef struct { | ||
FILE *f; | ||
int is_kind; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use an enum to be more readable please. |
||
char filename[64]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use JS__PATH_MAX to be safe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is using the tmp environment path size and malloc ... but I'm glad you told me about this. I can use that for os.exec command line buffer size? |
||
} JSTMPFile; | ||
#endif | ||
|
||
|
||
static bool is_stdio(FILE *f) | ||
{ | ||
return f == stdin || f == stdout || f == stderr; | ||
|
@@ -1044,17 +1053,26 @@ static void js_std_file_finalizer(JSRuntime *rt, JSValueConst val) | |
JSThreadState *ts = js_get_thread_state(rt); | ||
JSSTDFile *s = JS_GetOpaque(val, ts->std_file_class_id); | ||
if (s) { | ||
#if defined(__MINGW32__) || defined(__MINGW64__) | ||
JSTMPFile *ss = (JSTMPFile*) s; | ||
if (ss->is_kind==2) { | ||
if (ss->f) fclose(ss->f); | ||
if (ss->filename[0] != 0) remove(ss->filename); | ||
} else if (s->f && !is_stdio(s->f)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can just return here rather than having the check duplicated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean return and take the else out? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty much. Well, you may need some extra cleanup, or a goto end... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes I left it open for cleanup before. |
||
#else | ||
if (s->f && !is_stdio(s->f)) { | ||
#endif | ||
#if !defined(__wasi__) | ||
if (s->is_popen) | ||
pclose(s->f); | ||
else | ||
#endif | ||
fclose(s->f); | ||
|
||
} | ||
js_free_rt(rt, s); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
static ssize_t js_get_errno(ssize_t ret) | ||
{ | ||
|
@@ -1207,6 +1225,62 @@ static JSValue js_std_fdopen(JSContext *ctx, JSValueConst this_val, | |
} | ||
|
||
#if !defined(__wasi__) | ||
#if defined(__MINGW32__) || defined(__MINGW64__) | ||
// c:/tmp no longer works in windows. c:/ doesn't work. permissions! | ||
static JSValue js_std_tmpfile(JSContext *ctx, JSValueConst this_val, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, why are we doing all of this when MinGW already has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because it has been broken for years. It's hit and miss with microsoft compilers, because the internal windows code that MINGW calls is still there and that doesn't work. I researched it. It's a folder permission change in the operating system, in at least back to win10. None of my windows versions could run the test_std.js file because of that and the mtime problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the problem? msys2/MINGW-packages#18878 (comment) Perhaps it's time to default to the UCRT build. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is pretty clean now. I just tested all 3 builds and there's no compiler warnings. I wouldn't do that! |
||
int argc, JSValueConst *argv) | ||
{ | ||
JSRuntime *rt = JS_GetRuntime(ctx); | ||
JSThreadState *ts = js_get_thread_state(rt); | ||
JSTMPFile *s; | ||
JSValue obj; | ||
obj = JS_NewObjectClass(ctx, ts->std_file_class_id); | ||
if (JS_IsException(obj)) | ||
return obj; | ||
s = js_mallocz(ctx, sizeof(*s)); | ||
if (!s) { | ||
JS_FreeValue(ctx, obj); | ||
return JS_EXCEPTION; | ||
} | ||
|
||
char * env = getenv("TMP"); | ||
if (!env) env = getenv("TEMP"); | ||
int i = 0; | ||
if (env) { | ||
while (env[i]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make the buffer long enough or malloc it, this feels wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now that you mention it, since I've gone to the trouble of a special destructor, malloc is the right answer. The first time around I was using a different function that handled deleting the file, but then something else was wrong with that solution. I'm glad Bill Gates is gone! everyone's looking for a solution to this problem, it's actually not easy to get working. (that link wasn't what I thought). I maybe should be detecting the _WIN32 flag here because I'm not sure if Mingw in linux is going to to compile the wrong code here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hey sir! do you suggest using malloc? or do you suggest using js_malloc? I was afraid of thrashing the JS memory but I didn't know if there's concerns with the application or the memory management that requires js_malloc. |
||
s->filename[i] = env[i]; | ||
i++; | ||
if (i > 50) return JS_NULL; | ||
}; | ||
}; | ||
char* fname = &s->filename[i]; | ||
char* templ = "\\qXXXXXXX"; | ||
while (templ[0]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. memcpy? |
||
fname[0] = templ[0]; | ||
fname++; templ++; | ||
}; | ||
fname[0] = 0; | ||
int mkf = mkstemp(s->filename); | ||
if (mkf == -1) { | ||
JS_FreeValue(ctx, obj); | ||
js_free(ctx, s); | ||
return JS_NULL; | ||
}; | ||
int fd = dup(mkf); | ||
s->f = fdopen( fd, "a+"); | ||
close(mkf); | ||
if (argc >= 1) js_set_error_object(ctx, argv[0], s->f ? 0 : errno); | ||
if (!s->f) { | ||
JS_FreeValue(ctx, obj); | ||
js_free(ctx, s); | ||
return JS_NULL; | ||
}; | ||
|
||
s->is_kind = 2; | ||
JS_SetOpaque(obj, s); | ||
return obj; | ||
} | ||
#else // MINGW | ||
static JSValue js_std_tmpfile(JSContext *ctx, JSValueConst this_val, | ||
int argc, JSValueConst *argv) | ||
{ | ||
|
@@ -1218,7 +1292,8 @@ static JSValue js_std_tmpfile(JSContext *ctx, JSValueConst this_val, | |
return JS_NULL; | ||
return js_new_std_file(ctx, f, false); | ||
} | ||
#endif | ||
#endif // !MINGW | ||
#endif // WASI | ||
|
||
static JSValue js_std_sprintf(JSContext *ctx, JSValueConst this_val, | ||
int argc, JSValueConst *argv) | ||
|
@@ -3055,9 +3130,7 @@ static JSValue js_os_realpath(JSContext *ctx, JSValueConst this_val, | |
} | ||
return make_string_error(ctx, buf, err); | ||
} | ||
#endif | ||
|
||
#if !defined(_WIN32) && !defined(__wasi__) | ||
static JSValue js_os_symlink(JSContext *ctx, JSValueConst this_val, | ||
int argc, JSValueConst *argv) | ||
{ | ||
|
@@ -3072,12 +3145,27 @@ static JSValue js_os_symlink(JSContext *ctx, JSValueConst this_val, | |
JS_FreeCString(ctx, target); | ||
return JS_EXCEPTION; | ||
} | ||
#ifdef _WIN32 | ||
int isdirflag = 0; // might need to pass a value in for folders. | ||
if (argc > 2) { | ||
if (JS_ToInt32(ctx, &isdirflag, argv[2])) return JS_EXCEPTION; | ||
}; | ||
printf("symbolic link: %s -> %s; isdir: %d", target, linkpath, isdirflag); | ||
// 2 = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the constant rather than the value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had an error that it couldn't find header and just left it in the comment. |
||
err = CreateSymbolicLinkA(linkpath, target, ( isdirflag | 2 ) ) ; | ||
printf(" returned %d\r\n", err); | ||
if (!err) err = GetLastError(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: please use similar style to what we use all across the codebase |
||
else err = 0; | ||
#else | ||
err = js_get_errno(symlink(target, linkpath)); | ||
#endif | ||
JS_FreeCString(ctx, target); | ||
JS_FreeCString(ctx, linkpath); | ||
return JS_NewInt32(ctx, err); | ||
} | ||
#endif | ||
|
||
#if !defined(_WIN32) && !defined(__wasi__) | ||
/* return [path, errorcode] */ | ||
static JSValue js_os_readlink(JSContext *ctx, JSValueConst this_val, | ||
int argc, JSValueConst *argv) | ||
|
@@ -4113,10 +4201,10 @@ static const JSCFunctionListEntry js_os_funcs[] = { | |
JS_CFUNC_DEF("sleep", 1, js_os_sleep ), | ||
#if !defined(__wasi__) | ||
JS_CFUNC_DEF("realpath", 1, js_os_realpath ), | ||
JS_CFUNC_DEF("symlink", 2, js_os_symlink ), | ||
#endif | ||
#if !defined(_WIN32) && !defined(__wasi__) | ||
JS_CFUNC_MAGIC_DEF("lstat", 1, js_os_stat, 1 ), | ||
JS_CFUNC_DEF("symlink", 2, js_os_symlink ), | ||
JS_CFUNC_DEF("readlink", 1, js_os_readlink ), | ||
JS_CFUNC_DEF("exec", 1, js_os_exec ), | ||
JS_CFUNC_DEF("getpid", 0, js_os_getpid ), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,11 +168,19 @@ function test_os() | |
[st, err] = os.stat(fpath); | ||
assert(err, 0); | ||
assert(st.mode & os.S_IFMT, os.S_IFREG); | ||
assert(st.mtime, fdate); | ||
|
||
|
||
if (!isWin) { | ||
|
||
// I bet this can be fixed in windows 10 | ||
assert(st.mtime, fdate); | ||
|
||
/* symlink in windows 10+ requres admin or SeCreateSymbolicLinkPrivilege privilege found under: | ||
Computer Configuration\Windows Settings\Security Settings\Local Policies\User Rights Assignment\ | ||
*/ | ||
err = os.symlink(fname, link_path); | ||
assert(err, 0); | ||
|
||
|
||
[st, err] = os.lstat(link_path); | ||
assert(err, 0); | ||
|
@@ -298,3 +306,5 @@ test_interval(); | |
test_timeout(); | ||
test_timeout_order(); | ||
test_stdio_close(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drop the extra line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changes in as requested and tested. |
||
std.exit(0); | ||
coreyapowell marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't support the old MinGW32 so you can drop that part of the test.