mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-10 10:15:37 +02:00
Remove most assert()
calls (issue 8506)
This replaces `assert` calls with `throw new FormatError()`/`throw new Error()`. In a few places, throwing an `Error` (which is what `assert` meant) isn't correct since the enclosing function is supposed to return a `Promise`, hence some cases were changed to `Promise.reject(...)` and similarily for `createPromiseCapability` instances.
This commit is contained in:
parent
09f04eccda
commit
814fa1dee3
12 changed files with 193 additions and 89 deletions
|
@ -673,7 +673,9 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
|
||||
var fontRef, xref = this.xref;
|
||||
if (font) { // Loading by ref.
|
||||
assert(isRef(font));
|
||||
if (!isRef(font)) {
|
||||
throw new Error('The "font" object should be a reference.');
|
||||
}
|
||||
fontRef = font;
|
||||
} else { // Loading by name.
|
||||
var fontRes = resources.get('Font');
|
||||
|
@ -866,7 +868,9 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
resources = resources || Dict.empty;
|
||||
initialState = initialState || new EvalState();
|
||||
|
||||
assert(operatorList, 'getOperatorList: missing "operatorList" parameter');
|
||||
if (!operatorList) {
|
||||
throw new Error('getOperatorList: missing "operatorList" parameter');
|
||||
}
|
||||
|
||||
var self = this;
|
||||
var xref = this.xref;
|
||||
|
@ -928,10 +932,14 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
|
||||
var xobj = xobjs.get(name);
|
||||
if (xobj) {
|
||||
assert(isStream(xobj), 'XObject should be a stream');
|
||||
if (!isStream(xobj)) {
|
||||
throw new FormatError('XObject should be a stream');
|
||||
}
|
||||
|
||||
var type = xobj.dict.get('Subtype');
|
||||
assert(isName(type), 'XObject should have a Name subtype');
|
||||
if (!isName(type)) {
|
||||
throw new FormatError('XObject should have a Name subtype');
|
||||
}
|
||||
|
||||
if (type.name === 'Form') {
|
||||
stateManager.save();
|
||||
|
@ -1087,10 +1095,14 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
|
||||
case OPS.shadingFill:
|
||||
var shadingRes = resources.get('Shading');
|
||||
assert(shadingRes, 'No shading resource found');
|
||||
if (!shadingRes) {
|
||||
throw new FormatError('No shading resource found');
|
||||
}
|
||||
|
||||
var shading = shadingRes.get(args[0].name);
|
||||
assert(shading, 'No shading object found');
|
||||
if (!shading) {
|
||||
throw new FormatError('No shading object found');
|
||||
}
|
||||
|
||||
var shadingFill = Pattern.parseShading(shading, null, xref,
|
||||
resources, self.handler);
|
||||
|
@ -1636,10 +1648,14 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
if (!xobj) {
|
||||
break;
|
||||
}
|
||||
assert(isStream(xobj), 'XObject should be a stream');
|
||||
if (!isStream(xobj)) {
|
||||
throw new FormatError('XObject should be a stream');
|
||||
}
|
||||
|
||||
var type = xobj.dict.get('Subtype');
|
||||
assert(isName(type), 'XObject should have a Name subtype');
|
||||
if (!isName(type)) {
|
||||
throw new FormatError('XObject should have a Name subtype');
|
||||
}
|
||||
|
||||
if (type.name !== 'Form') {
|
||||
skipEmptyXObjs[name] = true;
|
||||
|
@ -1977,7 +1993,9 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
var cMap = properties.cMap;
|
||||
toUnicode = [];
|
||||
cMap.forEach(function(charcode, cid) {
|
||||
assert(cid <= 0xffff, 'Max size of CID is 65,535');
|
||||
if (cid > 0xffff) {
|
||||
throw new FormatError('Max size of CID is 65,535');
|
||||
}
|
||||
// e) Map the CID obtained in step (a) according to the CMap
|
||||
// obtained in step (d), producing a Unicode value.
|
||||
var ucs2 = ucs2CMap.lookup(cid);
|
||||
|
@ -2229,7 +2247,9 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
preEvaluateFont: function PartialEvaluator_preEvaluateFont(dict) {
|
||||
var baseDict = dict;
|
||||
var type = dict.get('Subtype');
|
||||
assert(isName(type), 'invalid font Subtype');
|
||||
if (!isName(type)) {
|
||||
throw new FormatError('invalid font Subtype');
|
||||
}
|
||||
|
||||
var composite = false;
|
||||
var uint8array;
|
||||
|
@ -2239,11 +2259,15 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
// - set the type according to the descendant font
|
||||
// - get the FontDescriptor from the descendant font
|
||||
var df = dict.get('DescendantFonts');
|
||||
assert(df, 'Descendant fonts are not specified');
|
||||
if (!df) {
|
||||
throw new FormatError('Descendant fonts are not specified');
|
||||
}
|
||||
dict = (isArray(df) ? this.xref.fetchIfRef(df[0]) : df);
|
||||
|
||||
type = dict.get('Subtype');
|
||||
assert(isName(type), 'invalid font Subtype');
|
||||
if (!isName(type)) {
|
||||
throw new FormatError('invalid font Subtype');
|
||||
}
|
||||
composite = true;
|
||||
}
|
||||
|
||||
|
@ -2331,7 +2355,9 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
// FontDescriptor was not required.
|
||||
// This case is here for compatibility.
|
||||
var baseFontName = dict.get('BaseFont');
|
||||
assert(isName(baseFontName), 'Base font is not specified');
|
||||
if (!isName(baseFontName)) {
|
||||
throw new FormatError('Base font is not specified');
|
||||
}
|
||||
|
||||
// Using base font name as a font name.
|
||||
baseFontName = baseFontName.name.replace(/[,_]/g, '-');
|
||||
|
@ -2398,7 +2424,9 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
}
|
||||
fontName = (fontName || baseFont);
|
||||
|
||||
assert(isName(fontName), 'invalid font name');
|
||||
if (!isName(fontName)) {
|
||||
throw new FormatError('invalid font name');
|
||||
}
|
||||
|
||||
var fontFile = descriptor.get('FontFile', 'FontFile2', 'FontFile3');
|
||||
if (fontFile) {
|
||||
|
@ -2494,7 +2522,9 @@ var TranslatedFont = (function TranslatedFontClosure() {
|
|||
this.sent = true;
|
||||
},
|
||||
loadType3Data(evaluator, resources, parentOperatorList, task) {
|
||||
assert(this.font.isType3Font);
|
||||
if (!this.font.isType3Font) {
|
||||
throw new Error('Must be a Type3 font.');
|
||||
}
|
||||
|
||||
if (this.type3Loaded) {
|
||||
return this.type3Loaded;
|
||||
|
@ -2997,7 +3027,9 @@ var EvaluatorPreprocessor = (function EvaluatorPreprocessorClosure() {
|
|||
args = [];
|
||||
}
|
||||
args.push(obj);
|
||||
assert(args.length <= 33, 'Too many arguments');
|
||||
if (args.length > 33) {
|
||||
throw new FormatError('Too many arguments');
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue