use defineProperty to shadow getters

This commit is contained in:
Andreas Gal 2011-05-13 00:19:50 -07:00
parent d8366507d0
commit a4c5b6095b
2 changed files with 17 additions and 9 deletions

17
pdf.js
View File

@ -12,9 +12,14 @@ function error(msg) {
throw new Error(msg); throw new Error(msg);
} }
function shadow(obj, prop, value) {
Object.defineProperty(obj, prop, { value: value, enumerable: true });
return value;
}
var Stream = (function() { var Stream = (function() {
function constructor(arrayBuffer) { function constructor(arrayBuffer) {
this.bytes = Uint8Array(arrayBuffer); this.bytes = new Uint8Array(arrayBuffer);
this.pos = 0; this.pos = 0;
this.start = 0; this.start = 0;
} }
@ -1857,14 +1862,14 @@ var Catalog = (function() {
if (!IsDict(obj)) if (!IsDict(obj))
error("invalid top-level pages dictionary"); error("invalid top-level pages dictionary");
// shadow the prototype getter // shadow the prototype getter
return this.toplevelPagesDict = obj; return shadow(this, "toplevelPagesDict", obj);
}, },
get numPages() { get numPages() {
obj = this.toplevelPagesDict.get("Count"); obj = this.toplevelPagesDict.get("Count");
if (!IsInt(obj)) if (!IsInt(obj))
error("page count in top level pages object is not an integer"); error("page count in top level pages object is not an integer");
// shadow the prototype getter // shadow the prototype getter
return this.numPages = obj; return shadow(this, "num", obj);
}, },
traverseKids: function(pagesDict) { traverseKids: function(pagesDict) {
var pageCache = this.pageCache; var pageCache = this.pageCache;
@ -1930,7 +1935,7 @@ var PDFDoc = (function() {
linearization = false; linearization = false;
} }
// shadow the prototype getter with a data property // shadow the prototype getter with a data property
return this.linearization = linearization; return shadow(this, "linearization", linearization);
}, },
get startXRef() { get startXRef() {
var stream = this.stream; var stream = this.stream;
@ -1963,7 +1968,7 @@ var PDFDoc = (function() {
} }
} }
// shadow the prototype getter with a data property // shadow the prototype getter with a data property
return this.startXRef = startXRef; return shadow(this, "startXRef", startXRef);
}, },
get mainXRefEntriesOffset() { get mainXRefEntriesOffset() {
var mainXRefEntriesOffset = 0; var mainXRefEntriesOffset = 0;
@ -1971,7 +1976,7 @@ var PDFDoc = (function() {
if (linearization) if (linearization)
mainXRefEntriesOffset = linearization.mainXRefEntriesOffset; mainXRefEntriesOffset = linearization.mainXRefEntriesOffset;
// shadow the prototype getter with a data property // shadow the prototype getter with a data property
return this.mainXRefEntriesOffset = mainXRefEntriesOffset; return shadow(this, "mainXRefEntriesOffset", mainXRefEntriesOffset);
}, },
// Find the header, remove leading garbage and setup the stream // Find the header, remove leading garbage and setup the stream
// starting from the header. // starting from the header.

View File

@ -29,15 +29,18 @@ function load() {
req = new XMLHttpRequest(); req = new XMLHttpRequest();
req.open("GET", "uncompressed.tracemonkey-pldi-09.pdf"); req.open("GET", "uncompressed.tracemonkey-pldi-09.pdf");
req.mozResponseType = "arraybuffer"; req.mozResponseType = req.responseType = "arraybuffer";
req.expected = 0; // 200 for HTTP req.expected = (document.URL.indexOf("file:") == 0) ? 0 : 200;
req.onreadystatechange = xhrstate; req.onreadystatechange = xhrstate;
req.send(null); req.send(null);
} }
function xhrstate() { function xhrstate() {
if (req.readyState == 4 && req.status == req.expected) { if (req.readyState == 4 && req.status == req.expected) {
var data = req.mozResponseArrayBuffer || req.mozResponse; var data = req.mozResponseArrayBuffer ||
req.mozResponse ||
req.responseArrayBuffer ||
req.response;
pdf = new PDFDoc(new Stream(data)); pdf = new PDFDoc(new Stream(data));
numPages = pdf.numPages; numPages = pdf.numPages;
displayPage(1); displayPage(1);