Skip to content Skip to sidebar Skip to footer

Firefox Extension With Js-ctypes On 32-bit And/or 64-bit Firefox (version 25.0.1)

I am creating a firefox extension for linux firefox on 64-bit ubuntu. The extension is a javascript program that calls functions in my shared library (libcog.so) written in C via

Solution 1:

js-ctypes really is just an abstraction on top of dlopen/LoadLibrary and dlsym/GetProcAddress.

  • Is javascript in a 64-bit browser running in 32-bit mode or 64-bit mode?

Right now, in Firefox, a 64-bit installation will only use 64-bit processes. In future versions, it is possible, but unlikely, that a 64-bit Firefox may create 32-bit child processes that therefore might embed a 32-bit js engine.

  • Does this question even make sense for interpreted languages like js?

Well, in general no. Javascript is designed to abstract away the bitness and even processor architecture. But when it comes to js-ctypes, it might matter, as the goal of js-ctypes is to escape the abstraction and associated limits of the JS VM/engine.

  • Should the js-ctypes mechanism even work in a 64-bit firefox browser?

js-ctypes works for 32-bit and 64-bit alike, and also for non x86 CPUs such as ARM. For example, the OS.File API, which comes with recent version of Firefox (and other mozilla-powered applications) does use js-ctypes internally, and is supported in x86 and ARM (well, *nix on ARM) alike.

However, while js-ctypes work, the binaries you interface with need to be platform specific, of course, and you need to correctly define their APIs (and ABI to some extend) in js-ctypes.

  • If so, should function protocol always be specified default_abi?

This depends on the ABI the library is actually using... For example, it is quite common for (commercial) library to use winapi_abi, because that is what windows uses for the system libraries. On *nix you'll mostly find cdecl/default_abi use, but a library author (e.g. you) is still free to use something else.

Either read the documentation for system and third party libraries, or when creating a library yourself, you should be already knowing what you're using.

  • If so, when javascript calls js-ctype library functions, is it: calling 32-bit functions in 32-bit libraries in /usr/lib32?, etc.
  • js-ctypes in a firefox extension in a 32-bit browser will always call functions in a 32-bit shared library in /usr/lib32 (or other 32-bit library directory)?
  • js-ctypes in a firefox extension in a 64-bit browser will always call functions in a 64-bit shared library in /usr/lib (or other 64-bit library directory)?

This depends on the system dynamic linker (and it's configuration). Most 32-bit operating systems won't even have a /usr/lib32 directory. x86_64 might have a 32-bit userland, but where it is located might different from distribution to distribution.

/usr/lib usually contains libraries native to the OS platform and bitness. x86 distributions will contain libraries for 32-bit Intel, x86_64 (AMD64) distributions for 64-bit Intel, ARM distribitions... you get the drift.

It is quite common for code using js-ctypes to try a couple of libraries, often with (somewhat) hard-coded paths.

For example, OS.File will attempt to loadlibc from a couple of hard-coded names, assuming the linker will figure out the library paths itself. Depending on the linker and other factors (32-bit Firefox on 64-bit OS), this might still fail, however.

In another example, in one of my own add-ons, I ship binaries for different platforms in the add-on package and just try all of them until one loads. That way I can support different platforms, in this example right now Win32, Win64, most gcc/ELF x86 *nix and most gcc/ELF x86_64 (AMD64) *nix. Of course, I had to make sure my library code is portable enough and does build correctly on different architectures and actually compile for all these targets.

I'd recommend you read up on shared object/dylib/DLL libraries and dynamic linking in general, because most parts of your question do not seem to be specific to js-ctypes, but more to library/linker/OSes in general.

Post a Comment for "Firefox Extension With Js-ctypes On 32-bit And/or 64-bit Firefox (version 25.0.1)"