Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(libc): windows native module support #490

Merged
merged 20 commits into from
Sep 10, 2024

Conversation

zeromake
Copy link
Contributor

@zeromake zeromake commented Aug 24, 2024

by #486

add definde QJS_NATIVE_MODULE_SUFFIX, windows is .dll, !windows is .so.

  • add cmake test

@zeromake
Copy link
Contributor Author

mingw it seems to be different from MSVC……

@saghul
Copy link
Contributor

saghul commented Aug 26, 2024

Yep, some if-defing might be required...

@zeromake
Copy link
Contributor Author

@saghul ci is ok

@saghul
Copy link
Contributor

saghul commented Aug 26, 2024

Can you make sure the relevant tests also run on Windows?

IIRC they are not compiled in.

@zeromake
Copy link
Contributor Author

@saghul
That's what you're talking about?

if(NOT WIN32)

init = (JSInitModuleFunc *)(void *)GetProcAddress(hd, "js_init_module");
if (!init) {
JS_ThrowReferenceError(ctx, "js_init_module '%s' not found: %lu",
module_name, GetLastError());
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be nicer to use FormatMessageA() to get a human-readable error message (example)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bnoordhuis
FormatMessageA error message fetching is a bit complicated, whether to consider a separate method to get it

struct JSContext {
    char* errmsg;
}
static int js_error(JSContext *ctx) {
    if (ctx->errmsg) {
    LocalFree(ctx->errmsg);
    ctx->errmsg = NULL;
  }
  // FormatMessageA ……
}

Copy link
Contributor

Choose a reason for hiding this comment

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

@bnoordhuis Doing it The Right Way (TM) seems a bit involved: https://github.com/libuv/libuv/blob/0a00e80c3686b93eccb9a801954e86bd7d7fe6ab/src/win/dl.c#L92 perhaps we can simplify here?

@saghul
Copy link
Contributor

saghul commented Aug 26, 2024

@saghul

That's what you're talking about?

if(NOT WIN32)

Yep those will do!

@zeromake
Copy link
Contributor Author

@saghul
Copy link
Contributor

saghul commented Aug 28, 2024

How weird, I'll take a look at that one. You can ignore it.

@saghul
Copy link
Contributor

saghul commented Sep 3, 2024

I guess what's missing is to run the actual tests, check this one:

- name: test examples

zeromake and others added 3 commits September 4, 2024 07:59
Co-authored-by: Ben Noordhuis <info@bnoordhuis.nl>
Co-authored-by: Ben Noordhuis <info@bnoordhuis.nl>
Co-authored-by: Ben Noordhuis <info@bnoordhuis.nl>
@zeromake
Copy link
Contributor Author

zeromake commented Sep 4, 2024

@saghul
job error:

/usr/bin/ld: libqjs.a(quickjs.c.o): relocation R_X86_64_PC32 against symbol `digits36' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value

linux build so file need -fPIC flags?

@saghul
Copy link
Contributor

saghul commented Sep 4, 2024

Not sure why that started happening, we need to take a look, but you can ignore that for this PR.

@zeromake
Copy link
Contributor Author

zeromake commented Sep 6, 2024

@saghul
c2391b0

  • windows shared use .so
  • fib.c use __declspec(dllexport) js_init_module
  • ci add examples\test_fib.js

@zeromake
Copy link
Contributor Author

zeromake commented Sep 7, 2024

@saghul
except for the wrong text, It should all be done, and you can consider whether to merge or not.

@zeromake
Copy link
Contributor Author

zeromake commented Sep 9, 2024

@saghul job/29858936901 What is wrong with this

@saghul
Copy link
Contributor

saghul commented Sep 9, 2024

Since you modified the JS part of the test, you need to commit the result of make codegen.

@chqrlie
Copy link
Collaborator

chqrlie commented Sep 9, 2024

Since you modified the JS part of the test, you need to commit the result of make codegen.

Yes, this is unfortunate. We should try and find a way to generate these files at build time and avoid committing them. This would also reduce bloat in the repository.

@saghul
Copy link
Contributor

saghul commented Sep 9, 2024

Yes, this is unfortunate. We should try and find a way to generate these files at build time and avoid committing them. This would also reduce bloat in the repository.

I have the same conundrum on txiki.js, only worse, because I have a lot more generated code.

I gave this a long hard thought and while we could generate it in the release job and then make a "release tarball", I'd rather have the git repo be the source of truth, specially after the xz fiasco.

@bnoordhuis
Copy link
Contributor

I concur with that sentiment, fwiw.

@saghul
Copy link
Contributor

saghul commented Sep 9, 2024

I concur with that sentiment, fwiw.

Which of the 2? 😅

@chqrlie
Copy link
Collaborator

chqrlie commented Sep 9, 2024

I gave this a long hard thought and while we could generate it in the release job and then make a "release tarball", I'd rather have the git repo be the source of truth, specially after the xz fiasco.

I am not sure I understand the connection with the xz fiasco. We shall be very cautious about accepting PRs from, let alone giving commit rights to outsiders.

Generating the byte code files at build time is mainly a problem for cross compiling where it requires building a qjs executable for the host in addition to cross compiling for the selected target. This used to be problematic because the generated files depended on the endianness of the target, which is no longer the case.

@bnoordhuis
Copy link
Contributor

Which of the 2? 😅

That git should be the Source Of Truth :)

(I deliberated on whether to mention I was referring to your comment but, since a couple of a minutes had passed, I figured it was unambiguously in response to the last comment.)

@saghul
Copy link
Contributor

saghul commented Sep 9, 2024

Let's continue here: #509 not to derail the PR.

@saghul
Copy link
Contributor

saghul commented Sep 9, 2024

@zeromake There seems to be a conflict. Can you please rebase with latest master?

@zeromake
Copy link
Contributor Author

zeromake commented Sep 9, 2024

@saghul is merged

Copy link
Contributor

@saghul saghul left a comment

Choose a reason for hiding this comment

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

Great work! @bnoordhuis can you PTAL too?

Copy link
Contributor

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

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

LGTM with some suggestions and questions.

@saghul
Copy link
Contributor

saghul commented Sep 10, 2024

LINK : fatal error LNK1149: output filename matches input filename 'D:\a\quickjs\quickjs\build\Release\qjs.lib' [D:\a\quickjs\quickjs\build\qjs_exe.vcxproj]

WTF?!

@zeromake
Copy link
Contributor Author

zeromake commented Sep 10, 2024

LINK : fatal error LNK1149: output filename matches input filename 'D:\a\quickjs\quickjs\build\Release\qjs.lib' [D:\a\quickjs\quickjs\build\qjs_exe.vcxproj]

WTF?!

i do rollback

+target_link_libraries(fib qjs)
-target_link_libraries(fib ${qjs_libs})

@zeromake
Copy link
Contributor Author

@saghul runs/10788294143 run on 3 hours ago……

@saghul
Copy link
Contributor

saghul commented Sep 10, 2024

I think our free credit is running low 😅

@saghul
Copy link
Contributor

saghul commented Sep 10, 2024

Hum I see the failure again. I think it might be the JS_EXTERN change. Can you undo that and let's check again?

@saghul
Copy link
Contributor

saghul commented Sep 10, 2024

Looking at what we do in libuv:

#ifndef UV_EXTERN
#ifdef _WIN32
  /* Windows - set up dll import/export decorators. */
# if defined(BUILDING_UV_SHARED)
    /* Building shared library. */
#   define UV_EXTERN __declspec(dllexport)
# elif defined(USING_UV_SHARED)
    /* Using shared library. */
#   define UV_EXTERN __declspec(dllimport)
# else
    /* Building static library. */
#   define UV_EXTERN /* nothing */
# endif
#elif __GNUC__ >= 4
# define UV_EXTERN __attribute__((visibility("default")))
#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550) /* Sun Studio >= 8 */
# define UV_EXTERN __global
#else
# define UV_EXTERN /* nothing */
#endif
#endif /* UV_EXTERN */

I think we should undo the JS_EXTERN change and likely do something like the above later on.

@saghul saghul merged commit 902cc2c into quickjs-ng:master Sep 10, 2024
50 checks passed
@saghul
Copy link
Contributor

saghul commented Sep 10, 2024

@bnoordhuis I'll make some follow-up refinements since I'm playing around with the linking libraries in another PR.

@saghul
Copy link
Contributor

saghul commented Sep 10, 2024

Nice work @zeromake ! 👏

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants