Skip to content

Commit

Permalink
Add support for tests on windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
ochameau committed Feb 6, 2013
1 parent 2d4cc6e commit db008d9
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.obj
*.exp
*.lib
5 changes: 2 additions & 3 deletions jscpptypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,8 @@ exports.CppObject = CppObject;

function declare(lib, name, returnType) {
let args = Array.slice(arguments, 3);
// XXX: jsapi isn't mangled on windows, but it won't necessary apply to all libs
// Remove that test and always mangle.
let symbol = Services.appinfo.OS == "WINNT" ? name : mangleFunction(name, args);
let ctypesArgs = args.map(convertToCtypes);
let symbol = mangleFunction(name, args);
try {
return lib.declare.apply(
lib,
Expand All @@ -129,6 +127,7 @@ function declare(lib, name, returnType) {
convertToCtypes(returnType)].concat(ctypesArgs)
);
} catch(e) {
// XXX: C++ doesn't seem to be mangled on windows?
try {
return lib.declare.apply(
lib,
Expand Down
Binary file added test/shared_libs/c.dll
Binary file not shown.
Binary file added test/shared_libs/cpp.dll
Binary file not shown.
12 changes: 9 additions & 3 deletions test/shared_libs/lib.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#include <stdlib.h>

long MyFunction(int *foo, int bar)
#if defined(WINNT)
#define MYLIB_API __declspec(dllexport)
#else
#define MYLIB_API
#endif

long MYLIB_API MyFunction(int *foo, int bar)
{
return *foo + bar;
}
Expand All @@ -10,15 +16,15 @@ struct sMyStruct {
};
typedef struct sMyStruct MyClass;

MyClass* GetObject(int attr)
MyClass MYLIB_API *GetObject(int attr)
{
MyClass* obj;
obj = (MyClass*) malloc(sizeof(MyClass));
obj->attr = attr;
return obj;
}

int GetObjectAttr(MyClass *obj)
int MYLIB_API GetObjectAttr(MyClass *obj)
{
return obj->attr;
}
Expand Down
16 changes: 10 additions & 6 deletions test/shared_libs/lib.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
#if defined(WINNT)
#define MYLIB_API __declspec(dllexport)
#else
#define MYLIB_API
#endif

long MyFunction(int *foo, int bar)
long MYLIB_API MyFunction(int *foo, int bar)
{
return *foo + bar;
}

class MyClass {
class MYLIB_API MyClass {
public:
long attr;
};

MyClass* GetObject(int attr)
MyClass MYLIB_API *GetObject(int attr)
{
MyClass *obj = new MyClass();
obj->attr = attr;
return obj;
}

long GetObjectAttr(MyClass *obj)
long MYLIB_API GetObjectAttr(MyClass *obj)
{
return obj->attr;
}

namespace MyNS {
namespace MySubNS {
long MyNSFunction(long foo)
long MYLIB_API MyNSFunction(long foo)
{
return foo + 1;
}
}
}

7 changes: 2 additions & 5 deletions test/shared_libs/make.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
cl /Folibc.obj /c lib.c /nologo
link /nologo /dll /out:libc.dll /implib:fooc.lib libc.obj

cl /Folibcpp.obj /c lib.cpp /nologo
link /nologo /dll /out:libcpp.dll /implib:foocpp.lib libcpp.obj
cl /DWINNT lib.c /Fec.dll /LD

cl /DWINNT lib.cpp /Fecpp.dll /LD
16 changes: 13 additions & 3 deletions test/test-jscpptypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ const { createFromURL } = require("sdk/test/tmp-file");

Cu.import("resource://gre/modules/ctypes.jsm");

const { XPCOMABI } = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime);
const { OS, XPCOMABI } = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime);
const ARCH = XPCOMABI.split("-")[0]; // x86, x86_64

function getSharedLib(name) {
let filename = "lib" + name + "-" + ARCH + ".so";
let filename = ctypes.libraryName(
name + (OS == "Linux" ? "-" + ARCH: "")
);
console.log(">> "+filename);
let url = module.uri.replace("test-jscpptypes.js", "shared_libs/" + filename);
return createFromURL(url, filename);
}
Expand All @@ -28,23 +31,30 @@ exports["test C library"] = function (assert) {
let GetObjectAttr = declare(lib, "GetObjectAttr", ctypes.int, MyClass);
let attr = GetObjectAttr(obj);
assert.equal(attr, 42, "GetObjectAttr works");
lib.close();
}

exports["test C++ library"] = function (assert) {
let path = getSharedLib("cpp");
let lib = ctypes.open(path);
/*
let MyFunction = declare(lib, "MyFunction", ctypes.long, ctypes.int.ptr, ctypes.int);
let i = ctypes.int(1);
let rv = MyFunction(i.address(), 2);
assert.equal(rv, 3, "MyFunction works");

*/
let MyClass = CppObject("MyClass");
let GetObject = declare(lib, "GetObject", MyClass, ctypes.int);
let obj = GetObject(42);
assert.ok(parseInt(obj), "GetObject doesn't return a null pointer");
let GetObjectAttr = declare(lib, "GetObjectAttr", ctypes.int, MyClass);
let attr = GetObjectAttr(obj);
assert.equal(attr, 42, "GetObjectAttr works");

let MyNSFunction = declare(lib, "MyNS::MySubNS::MyNSFunction", ctypes.long, ctypes.long);
let rv = MyNSFunction(1);
assert.equal(rv, 2, "MyNSFunction works");
lib.close();
}

require('sdk/test').run(exports);
Expand Down

0 comments on commit db008d9

Please sign in to comment.