2012-08-19 12:00:19 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict"
|
|
|
|
|
2012-10-25 11:45:14 -07:00
|
|
|
const Cu = Components.utils;
|
2012-08-19 12:00:19 -07:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["ObjectWrapper"];
|
2012-08-19 12:00:19 -07:00
|
|
|
|
|
|
|
// Makes sure that we expose correctly chrome JS objects to content.
|
|
|
|
|
2013-12-04 08:53:21 -08:00
|
|
|
const TypedArrayThings = [
|
|
|
|
"Int8Array",
|
|
|
|
"Uint8Array",
|
|
|
|
"Uint8ClampedArray",
|
|
|
|
"Int16Array",
|
|
|
|
"Uint16Array",
|
|
|
|
"Int32Array",
|
|
|
|
"Uint32Array",
|
|
|
|
"Float32Array",
|
|
|
|
"Float64Array",
|
|
|
|
];
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.ObjectWrapper = {
|
2012-12-11 00:30:53 -08:00
|
|
|
getObjectKind: function objWrapper_getObjectKind(aObject) {
|
2012-12-12 14:58:11 -08:00
|
|
|
if (aObject === null || aObject === undefined) {
|
|
|
|
return "primitive";
|
|
|
|
} else if (Array.isArray(aObject)) {
|
2012-10-25 11:45:14 -07:00
|
|
|
return "array";
|
2012-12-11 00:30:53 -08:00
|
|
|
} else if (aObject instanceof Ci.nsIDOMFile) {
|
|
|
|
return "file";
|
|
|
|
} else if (aObject instanceof Ci.nsIDOMBlob) {
|
2012-10-25 11:45:14 -07:00
|
|
|
return "blob";
|
2012-12-16 21:29:00 -08:00
|
|
|
} else if (aObject instanceof Date) {
|
|
|
|
return "date";
|
2013-12-04 08:53:21 -08:00
|
|
|
} else if (TypedArrayThings.indexOf(aObject.constructor.name) !== -1) {
|
|
|
|
return aObject.constructor.name;
|
2012-10-25 11:45:14 -07:00
|
|
|
} else if (typeof aObject == "object") {
|
|
|
|
return "object";
|
|
|
|
} else {
|
|
|
|
return "primitive";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-19 12:00:19 -07:00
|
|
|
wrap: function objWrapper_wrap(aObject, aCtxt) {
|
2012-10-25 11:45:14 -07:00
|
|
|
// First check wich kind of object we have.
|
|
|
|
let kind = this.getObjectKind(aObject);
|
2012-12-12 14:58:11 -08:00
|
|
|
if (kind == "array") {
|
2012-10-25 11:45:14 -07:00
|
|
|
let res = Cu.createArrayIn(aCtxt);
|
|
|
|
aObject.forEach(function(aObj) {
|
|
|
|
res.push(this.wrap(aObj, aCtxt));
|
|
|
|
}, this);
|
|
|
|
return res;
|
2013-12-04 08:53:21 -08:00
|
|
|
} else if (TypedArrayThings.indexOf(kind) !== -1) {
|
|
|
|
// This is slow, because from the perspective of the constructor in aCtxt
|
|
|
|
// aObject is a CCW, and it gets the indexed properties one by one rather
|
|
|
|
// instead of realizing that this is already a typed array thing.
|
|
|
|
return new aCtxt[kind](aObject);
|
2012-12-11 00:30:53 -08:00
|
|
|
} else if (kind == "file") {
|
|
|
|
return new aCtxt.File(aObject,
|
|
|
|
{ name: aObject.name,
|
|
|
|
type: aObject.type });
|
2012-10-25 11:45:14 -07:00
|
|
|
} else if (kind == "blob") {
|
2012-12-15 02:09:02 -08:00
|
|
|
return new aCtxt.Blob([aObject], { type: aObject.type });
|
2012-12-16 21:29:00 -08:00
|
|
|
} else if (kind == "date") {
|
|
|
|
return Cu.createDateIn(aCtxt, aObject.getTime());
|
2012-10-25 11:45:14 -07:00
|
|
|
} else if (kind == "primitive") {
|
|
|
|
return aObject;
|
|
|
|
}
|
|
|
|
|
2012-12-11 00:30:53 -08:00
|
|
|
// Fall-through, we now have a dictionnary object.
|
2012-08-19 12:00:19 -07:00
|
|
|
let res = Cu.createObjectIn(aCtxt);
|
|
|
|
let propList = { };
|
|
|
|
for (let prop in aObject) {
|
|
|
|
propList[prop] = {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
2012-12-11 00:30:53 -08:00
|
|
|
value: this.wrap(aObject[prop], aCtxt)
|
2012-08-19 12:00:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Object.defineProperties(res, propList);
|
|
|
|
Cu.makeObjectPropsNormal(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|