Skip to content
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

Moved Window.__nidium__ object creation to a seperate method. #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions src/Binding/JSWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ JSWindow *JSWindow::GetObject(NidiumJS *njs)
JSClass *JSWindow::GetJSClass()
{
static JSClass global_class = {
"Window",
"Window",
JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(16) | JSCLASS_HAS_PRIVATE,
nullptr, nullptr,
nullptr, nullptr,
Expand All @@ -1149,7 +1149,7 @@ JSClass *JSWindow::GetJSClass()
nullptr, nullptr,
nullptr, JS_GlobalObjectTraceHook
};

return &global_class;
}

Expand Down Expand Up @@ -1196,26 +1196,11 @@ JSFunctionSpec *JSWindow::ListMethods()
return funcs;
}

JSWindow *JSWindow::RegisterObject(JSContext *cx,
int width,
int height,
JS::HandleObject docObj)
void JSWindow::RegisterNidiumObject(JSContext *cx)
{
JS::RootedObject globalObj(cx, JS::CurrentGlobalOrNull(cx));
JS::RootedObject windowObj(cx, globalObj);

JSWindow *jwin = new JSWindow(NidiumJS::GetObject(cx));

JSWindow::AssociateObject(cx, jwin, globalObj, true);
jwin->setUniqueInstance();

jwin->createMainCanvas(width, height, docObj);

// Set the __nidium__ properties
JS::RootedObject windowObj(cx, JS::CurrentGlobalOrNull(cx));
JS::RootedObject nidiumObj(cx, JS_NewPlainObject(cx));

JS::RootedValue val(cx);

JS::RootedString jVersion(cx, JS_NewStringCopyZ(cx, NIDIUM_VERSION_STR));
JS_DefineProperty(cx, nidiumObj, "version", jVersion,
JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY);
Expand All @@ -1228,10 +1213,27 @@ JSWindow *JSWindow::RegisterObject(JSContext *cx,
JS_DefineProperty(cx, nidiumObj, "revision", jRevision,
JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY);

val = JS::ObjectValue(*nidiumObj);
JS::RootedValue val(cx, JS::ObjectValue(*nidiumObj));
JS_SetProperty(cx, windowObj, "__nidium__", val);
}

JSWindow *JSWindow::RegisterObject(JSContext *cx,
int width,
int height,
JS::HandleObject docObj)
{
JS::RootedObject globalObj(cx, JS::CurrentGlobalOrNull(cx));
JS::RootedObject windowObj(cx, globalObj);

JSWindow *jwin = new JSWindow(NidiumJS::GetObject(cx));

JSWindow::AssociateObject(cx, jwin, globalObj, true);
jwin->setUniqueInstance();

jwin->createMainCanvas(width, height, docObj);
jwin->RegisterNidiumObject(cx);

#if 0
#if 0
//@TODO: intented to be used in a future
JS::RootedObject titleBar(cx, JSCanvas::GenerateJSObject(cx, width, 35));
static_cast<CanvasHandler *>(JS_GetPrivate(canvas))->translate(0, 35);
Expand Down
2 changes: 1 addition & 1 deletion src/Binding/JSWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class JSWindow : public ClassMapperWithEvents<JSWindow>

private:
bool dragEvent(const char *name, int x, int y);

static void RegisterNidiumObject(JSContext *cx);
void createMainCanvas(int width, int height, JS::HandleObject docObj);

struct _requestedFrame
Expand Down
20 changes: 20 additions & 0 deletions tests/jsunittest/Window/__nidium__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2016 Nidium Inc. All rights reserved.
Use of this source code is governed by a MIT license
that can be found in the LICENSE file.
*/

Tests.register("Window.__nidium__", function() {
var nidium = window.__nidium__;
console.log(nidium.build.length);

Assert.equal(typeof nidium, "object", "Window.__nidium__ should be a object");
Assert.equal(typeof nidium.version, "string", "__nidium__.version should be \"string\"");
Assert.notEqual(nidium.version.indexOf('.'), -1, "__nidium__.version should have at least one '.'");
Assert.equal(typeof nidium.build, "string", "__nidium__.build should be \"string\"");
Assert.equal(nidium.build.length, 40, "__nidium__.build should be 40 characters long.");
Assert.equal(typeof nidium.revision, "string", "__nidium__.revision should be \"string\"");
Assert.equal(nidium.revision.length, 40, "__nidium__.revision should be 40 characters long.");
});


1 change: 1 addition & 0 deletions tests/jsunittest/unittests.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ if (args["file"]) {
'Image.js',
'Canvas.js',
'DB.js', // Only for frontend, because server does not support "cache://"
'Window/__nidium__.js',
]);
}
};
Expand Down