@dmail/uneval
Advanced tools
Comparing version 5.3.0 to 5.4.0
@@ -158,41 +158,28 @@ 'use strict'; | ||
var propertiesDescription = {}; | ||
var propertyDescriptionArray = []; | ||
Object.getOwnPropertyNames(value).forEach(function (propertyName) { | ||
var propertyDescriptor = Object.getOwnPropertyDescriptor(value, propertyName); | ||
var propertyDescription = {}; | ||
Object.keys(propertyDescriptor).forEach(function (descriptorName) { | ||
var descriptorValue = propertyDescriptor[descriptorName]; | ||
if (descriptorName === "set" && descriptorValue && !functionAllowed) throw new Error(createForbiddenPropertySetterMessage({ | ||
path: path, | ||
propertyName: propertyName | ||
})); | ||
if (descriptorName === "get" && descriptorValue && !functionAllowed) throw new Error(createForbiddenPropertyGetterMessage({ | ||
path: path, | ||
propertyName: propertyName | ||
})); | ||
var descriptorNameIdentifier = valueToIdentifier(descriptorName, _toConsumableArray(path).concat(["[\"".concat(propertyName, "\"]"), "[[propertyDescriptor:".concat(descriptorName, "]]")])); | ||
var descriptorValueIdentifier = valueToIdentifier(descriptorValue, _toConsumableArray(path).concat(["[\"".concat(propertyName, "\"]"), "[[propertyDescriptor:".concat(descriptorName, "]]")])); | ||
propertyDescription[descriptorNameIdentifier] = descriptorValueIdentifier; | ||
var propertyNameIdentifier = valueToIdentifier(propertyName, _toConsumableArray(path).concat([propertyName])); | ||
var propertyDescription = computePropertyDescription(propertyDescriptor, propertyName, path); | ||
propertyDescriptionArray.push({ | ||
propertyNameIdentifier: propertyNameIdentifier, | ||
propertyDescription: propertyDescription | ||
}); | ||
var propertyNameIdentifier = valueToIdentifier(propertyName, _toConsumableArray(path).concat([propertyName])); | ||
propertiesDescription[propertyNameIdentifier] = propertyDescription; | ||
}); | ||
var symbolsDescription = {}; | ||
var symbolDescriptionArray = []; | ||
Object.getOwnPropertySymbols(value).forEach(function (symbol) { | ||
var propertyDescriptor = Object.getOwnPropertyDescriptor(value, symbol); | ||
var propertyDescription = {}; | ||
Object.keys(propertyDescriptor).forEach(function (descriptorName) { | ||
var descriptorNameIdentifier = valueToIdentifier(descriptorName, _toConsumableArray(path).concat(["[".concat(symbol.toString(), "]"), "[[propertyDescriptor:".concat(descriptorName, "]]")])); | ||
var descriptorValueIdentifier = valueToIdentifier(propertyDescriptor[descriptorName], _toConsumableArray(path).concat(["[".concat(symbol.toString(), "]"), "[[propertyDescriptor:".concat(descriptorName, "]]")])); | ||
propertyDescription[descriptorNameIdentifier] = descriptorValueIdentifier; | ||
var symbolIdentifier = valueToIdentifier(symbol, _toConsumableArray(path).concat(["[".concat(symbol.toString(), "]")])); | ||
var propertyDescription = computePropertyDescription(propertyDescriptor, symbol, path); | ||
symbolDescriptionArray.push({ | ||
symbolIdentifier: symbolIdentifier, | ||
propertyDescription: propertyDescription | ||
}); | ||
var symbolIdentifier = valueToIdentifier(symbol, _toConsumableArray(path).concat([symbol])); | ||
symbolsDescription[symbolIdentifier] = propertyDescription; | ||
}); | ||
var methodsDescription = computeMethodsDescription(value, path); | ||
var methodDescriptionArray = computeMethodDescriptionArray(value, path); | ||
var extensible = Object.isExtensible(value); | ||
recipeArray[identifier] = createCompositeRecipe({ | ||
propertiesDescription: propertiesDescription, | ||
symbolsDescription: symbolsDescription, | ||
methodsDescription: methodsDescription, | ||
propertyDescriptionArray: propertyDescriptionArray, | ||
symbolDescriptionArray: symbolDescriptionArray, | ||
methodDescriptionArray: methodDescriptionArray, | ||
extensible: extensible | ||
@@ -203,25 +190,51 @@ }); | ||
var computeMethodsDescription = function computeMethodsDescription(value, path) { | ||
var methodsDescription = {}; | ||
var computePropertyDescription = function computePropertyDescription(propertyDescriptor, propertyNameOrSymbol, path) { | ||
if (propertyDescriptor.set && !functionAllowed) throw new Error(createForbiddenPropertySetterMessage({ | ||
path: path, | ||
propertyNameOrSymbol: propertyNameOrSymbol | ||
})); | ||
if (propertyDescriptor.get && !functionAllowed) throw new Error(createForbiddenPropertyGetterMessage({ | ||
path: path, | ||
propertyNameOrSymbol: propertyNameOrSymbol | ||
})); | ||
return { | ||
configurable: propertyDescriptor.configurable, | ||
writable: propertyDescriptor.writable, | ||
enumerable: propertyDescriptor.enumerable, | ||
getIdentifier: "get" in propertyDescriptor ? valueToIdentifier(propertyDescriptor.get, _toConsumableArray(path).concat([String(propertyNameOrSymbol), "[[descriptor:get]]"])) : undefined, | ||
setIdentifier: "set" in propertyDescriptor ? valueToIdentifier(propertyDescriptor.set, _toConsumableArray(path).concat([String(propertyNameOrSymbol), "[[descriptor:set]]"])) : undefined, | ||
valueIdentifier: "value" in propertyDescriptor ? valueToIdentifier(propertyDescriptor.value, _toConsumableArray(path).concat([String(propertyNameOrSymbol), "[[descriptor:value]]"])) : undefined | ||
}; | ||
}; | ||
var computeMethodDescriptionArray = function computeMethodDescriptionArray(value, path) { | ||
var methodDescriptionArray = []; | ||
if (typeof Set === "function" && value instanceof Set) { | ||
var addCallArray = []; | ||
var callArray = []; | ||
value.forEach(function (entryValue, index) { | ||
var entryValueIdentifier = valueToIdentifier(entryValue, _toConsumableArray(path).concat(["[[SetEntryValue]]", index])); | ||
addCallArray.push([entryValueIdentifier]); | ||
callArray.push([entryValueIdentifier]); | ||
}); | ||
methodsDescription[valueToIdentifier("add")] = addCallArray; | ||
methodDescriptionArray.push({ | ||
methodNameIdentifier: valueToIdentifier("add"), | ||
callArray: callArray | ||
}); | ||
} | ||
if (typeof Map === "function" && value instanceof Map) { | ||
var setCallArray = []; | ||
var _callArray = []; | ||
value.forEach(function (entryValue, entryKey) { | ||
var entryKeyIdentifier = valueToIdentifier(entryKey, _toConsumableArray(path).concat(["[[MapEntryKey]]", entryKey])); | ||
var entryValueIdentifier = valueToIdentifier(entryValue, _toConsumableArray(path).concat(["[[MapEntryValue]]", entryValue])); | ||
setCallArray.push([entryKeyIdentifier, entryValueIdentifier]); | ||
_callArray.push([entryKeyIdentifier, entryValueIdentifier]); | ||
}); | ||
methodsDescription[valueToIdentifier("set")] = setCallArray; | ||
methodDescriptionArray.push({ | ||
methodNameIdentifier: valueToIdentifier("set"), | ||
callArray: _callArray | ||
}); | ||
} | ||
return methodsDescription; | ||
return methodDescriptionArray; | ||
}; | ||
@@ -327,3 +340,4 @@ | ||
recipeArray: recipeArray, | ||
mainIdentifier: mainIdentifier | ||
mainIdentifier: mainIdentifier, | ||
valueMap: valueMap | ||
}; | ||
@@ -372,5 +386,5 @@ }; | ||
valueOfIdentifier = _ref2.valueOfIdentifier, | ||
propertiesDescription = _ref2.propertiesDescription, | ||
symbolsDescription = _ref2.symbolsDescription, | ||
methodsDescription = _ref2.methodsDescription, | ||
propertyDescriptionArray = _ref2.propertyDescriptionArray, | ||
symbolDescriptionArray = _ref2.symbolDescriptionArray, | ||
methodDescriptionArray = _ref2.methodDescriptionArray, | ||
extensible = _ref2.extensible; | ||
@@ -381,5 +395,5 @@ return { | ||
valueOfIdentifier: valueOfIdentifier, | ||
propertiesDescription: propertiesDescription, | ||
symbolsDescription: symbolsDescription, | ||
methodsDescription: methodsDescription, | ||
propertyDescriptionArray: propertyDescriptionArray, | ||
symbolDescriptionArray: symbolDescriptionArray, | ||
methodDescriptionArray: methodDescriptionArray, | ||
extensible: extensible | ||
@@ -415,4 +429,4 @@ }; | ||
var path = _ref7.path, | ||
propertyName = _ref7.propertyName; | ||
return "property getter are not allowed.\ngetter found on property: ".concat(propertyName, "\nat: ").concat(path.join("")); | ||
propertyNameOrSymbol = _ref7.propertyNameOrSymbol; | ||
return "property getter are not allowed.\ngetter found on property: ".concat(String(propertyNameOrSymbol), "\nat: ").concat(path.join("")); | ||
}; | ||
@@ -422,4 +436,4 @@ | ||
var path = _ref8.path, | ||
propertyName = _ref8.propertyName; | ||
return "property setter are not allowed.\nsetter found on property: ".concat(propertyName, "\nat: ").concat(path.join("")); | ||
propertyNameOrSymbol = _ref8.propertyNameOrSymbol; | ||
return "property setter are not allowed.\nsetter found on property: ".concat(String(propertyNameOrSymbol), "\nat: ").concat(path.join("")); | ||
}; | ||
@@ -443,6 +457,3 @@ | ||
// when used internally by uneval | ||
var compositionToRecipe = function compositionToRecipe(_ref) { | ||
var recipeArray = _ref.recipeArray, | ||
mainIdentifier = _ref.mainIdentifier; | ||
var sortRecipe = function sortRecipe(recipeArray) { | ||
var findInRecipePrototypeChain = function findInRecipePrototypeChain(recipe, callback) { | ||
@@ -494,228 +505,5 @@ var currentRecipe = recipe; // eslint-disable-next-line no-constant-condition | ||
}); | ||
var moves = {}; | ||
recipeArray.forEach(function (recipe, index) { | ||
var indexOrdered = recipeArrayOrdered.indexOf(recipe); | ||
if (index !== indexOrdered) { | ||
moves[index] = indexOrdered; | ||
} | ||
}); | ||
var remapIdentifier = function remapIdentifier(identifier) { | ||
if (identifier in moves) return moves[identifier]; | ||
return identifier; | ||
}; | ||
var remapPropertyDescription = function remapPropertyDescription(propertyDescription) { | ||
var remappedPropertyDescription = {}; | ||
Object.keys(propertyDescription).forEach(function (descriptorNameIdentifier) { | ||
remappedPropertyDescription[remapIdentifier(descriptorNameIdentifier)] = remapIdentifier(propertyDescription[descriptorNameIdentifier]); | ||
}); | ||
return remappedPropertyDescription; | ||
}; | ||
recipeArrayOrdered.forEach(function (recipe) { | ||
if (recipe.type !== "composite") return; | ||
recipe.valueOfIdentifier = remapIdentifier(recipe.valueOfIdentifier); | ||
recipe.prototypeIdentifier = remapIdentifier(recipe.prototypeIdentifier); | ||
var propertiesDescription = recipe.propertiesDescription; | ||
var remappedPropertiesDescription = {}; | ||
Object.keys(propertiesDescription).forEach(function (propertyNameIdentifier) { | ||
remappedPropertiesDescription[remapIdentifier(propertyNameIdentifier)] = remapPropertyDescription(propertiesDescription[propertyNameIdentifier]); | ||
}); | ||
recipe.propertiesDescription = remappedPropertiesDescription; | ||
var symbolsDescription = recipe.symbolsDescription; | ||
var remappedSymbolsDescription = {}; | ||
Object.keys(symbolsDescription).forEach(function (symbolIdentifier) { | ||
remappedSymbolsDescription[remapIdentifier(symbolIdentifier)] = remapPropertyDescription(symbolsDescription[symbolIdentifier]); | ||
}); | ||
recipe.symbolsDescription = remappedSymbolsDescription; | ||
var methodsDescription = recipe.methodsDescription; | ||
var remappedMethodsDescription = {}; | ||
Object.keys(methodsDescription).forEach(function (methodNameIdentifier) { | ||
var callsDescription = methodsDescription[methodNameIdentifier]; | ||
var remappedCallsDescription = callsDescription.map(function (argumentIdentifiers) { | ||
return argumentIdentifiers.map(function (argumentIdentifier) { | ||
return remapIdentifier(argumentIdentifier); | ||
}); | ||
}); | ||
remappedMethodsDescription[remapIdentifier(methodNameIdentifier)] = remappedCallsDescription; | ||
}); | ||
recipe.methodsDescription = remappedMethodsDescription; | ||
}); | ||
return { | ||
recipeArray: recipeArrayOrdered, | ||
mainIdentifier: remapIdentifier(mainIdentifier) | ||
}; | ||
return recipeArrayOrdered; | ||
}; | ||
// This function would return recipes that does not contain default values | ||
// for instance we'll consider extensible as false by default | ||
// empty propertiesMap and symbolsMap can be omitted | ||
// it would decrease the size of value string representation a bit | ||
var minifyRecipe = function minifyRecipe(_ref) { | ||
var recipeArray = _ref.recipeArray, | ||
mainIdentifier = _ref.mainIdentifier; | ||
recipeArray.forEach(function (recipe) { | ||
if (recipe.type === "composite") { | ||
if (recipe.prototypeIdentifier === undefined) delete recipe.prototypeIdentifier; | ||
if (recipe.valueOfIdentifier === undefined) delete recipe.valueOfIdentifier; | ||
if (recipe.extensible === true) delete recipe.extensible; | ||
if (Object.keys(recipe.propertiesDescription).length === 0) delete recipe.propertiesDescription; | ||
if (Object.keys(recipe.symbolsDescription).length === 0) delete recipe.symbolsDescription; | ||
if (Object.keys(recipe.methodsDescription).length === 0) delete recipe.methodsDescription; | ||
} | ||
}); | ||
return { | ||
recipeArray: recipeArray, | ||
mainIdentifier: mainIdentifier | ||
}; | ||
}; | ||
function recompose(param) { | ||
var recipeArray = param.recipeArray; | ||
var mainIdentifier = param.mainIdentifier; | ||
var materials = {}; // eslint-disable-next-line no-extend-native | ||
Object.defineProperty(Object.prototype, "__global__", { | ||
// eslint-disable-next-line object-shorthand | ||
get: function get() { | ||
return this; | ||
}, | ||
configurable: true | ||
}); // eslint-disable-next-line no-undef | ||
var globalObject = __global__; | ||
delete Object.prototype.__global__; | ||
function setupMaterial(recipe) { | ||
if (recipe.type === "primitive") return setupPrimitiveMaterial(recipe); | ||
if (recipe.type === "global-symbol") return setupGlobalSymbolMaterial(recipe); | ||
if (recipe.type === "global-reference") return setupGlobalReferenceMaterial(recipe); | ||
return setupCompositeMaterial(recipe); | ||
} | ||
function setupPrimitiveMaterial(recipe) { | ||
return recipe.value; | ||
} | ||
function setupGlobalSymbolMaterial(recipe) { | ||
return Symbol.for(recipe.key); | ||
} | ||
function setupGlobalReferenceMaterial(recipe) { | ||
var path = recipe.path; | ||
var currentValue = globalObject; | ||
var i = 0; | ||
while (i < path.length) { | ||
var part = path[i]; | ||
i++; | ||
if (part in currentValue === false) throw new Error(createValueNotFoundErrorMessage({ | ||
path: path, | ||
index: i | ||
})); | ||
currentValue = currentValue[part]; | ||
} | ||
return currentValue; | ||
} | ||
function setupCompositeMaterial(recipe) { | ||
var prototypeIdentifier = recipe.prototypeIdentifier; | ||
var valueOfIdentifier = recipe.valueOfIdentifier; // regexp and function | ||
if (prototypeIdentifier === undefined) return materials[valueOfIdentifier]; | ||
var prototypeValue = materials[prototypeIdentifier]; | ||
if (prototypeValue === null) return Object.create(null); | ||
var Constructor = prototypeValue.constructor; | ||
if (Constructor === Object) return Object.create(prototypeValue); | ||
if (valueOfIdentifier === undefined) return new Constructor(); | ||
var valueOfValue = materials[valueOfIdentifier]; | ||
return new Constructor(valueOfValue); | ||
} | ||
recipeArray.forEach(function (recipe, index) { | ||
var value = setupMaterial(recipe); | ||
materials[index] = value; | ||
}); | ||
function followRecipe(recipe, index) { | ||
if (recipe.type === "composite") followCompositeRecipe(recipe, index); | ||
} | ||
function followCompositeRecipe(recipe, index) { | ||
var composite = materials[index]; | ||
var propertiesDescription = recipe.propertiesDescription; | ||
if (propertiesDescription) { | ||
Object.keys(propertiesDescription).forEach(function (propertyNameIdentifier) { | ||
var propertyName = materials[propertyNameIdentifier]; | ||
var description = propertiesDescription[propertyNameIdentifier]; | ||
defineProperty(composite, propertyName, description); | ||
}); | ||
} | ||
var symbolsDescription = recipe.symbolsDescription; | ||
if (symbolsDescription) { | ||
Object.keys(symbolsDescription).forEach(function (symbolIdentifier) { | ||
var symbol = materials[symbolIdentifier]; | ||
var description = symbolsDescription[symbolIdentifier]; | ||
defineProperty(composite, symbol, description); | ||
}); | ||
} | ||
var methodsDescription = recipe.methodsDescription; | ||
if (methodsDescription) { | ||
Object.keys(methodsDescription).forEach(function (methodNameIdentifier) { | ||
var methodName = materials[methodNameIdentifier]; | ||
var calls = methodsDescription[methodNameIdentifier]; | ||
calls.forEach(function (argumentIdentifiers) { | ||
var argumentValues = argumentIdentifiers.map(function (argumentIdentifier) { | ||
return materials[argumentIdentifier]; | ||
}); | ||
composite[methodName].apply(composite, _toConsumableArray(argumentValues)); | ||
}); | ||
}); | ||
} | ||
var extensible = recipe.extensible || true; | ||
if (!extensible) Object.preventExtensions(composite); | ||
} | ||
function defineProperty(composite, propertyNameOrSymbol, propertyDescription) { | ||
var currentDescriptor = Object.getOwnPropertyDescriptor(composite, propertyNameOrSymbol); | ||
if (currentDescriptor && currentDescriptor.configurable === false) return; | ||
var descriptor = {}; | ||
Object.keys(propertyDescription).forEach(function (descriptorNameIdentifier) { | ||
var descriptorValueIdentifier = propertyDescription[descriptorNameIdentifier]; | ||
var descriptorName = materials[descriptorNameIdentifier]; | ||
var descriptorValue = materials[descriptorValueIdentifier]; | ||
descriptor[descriptorName] = descriptorValue; | ||
}); | ||
Object.defineProperty(composite, propertyNameOrSymbol, descriptor); | ||
} | ||
recipeArray.forEach(function (recipe, index) { | ||
followRecipe(recipe, index); | ||
}); | ||
function createValueNotFoundErrorMessage(data) { | ||
var path = data.path; | ||
var index = data.index; | ||
var message = "value not found for path."; | ||
message += "\n"; // eslint-disable-next-line prefer-template | ||
message += "part not found: " + path[index]; | ||
message += "\n"; // eslint-disable-next-line prefer-template | ||
message += "path: " + path.join(""); | ||
return message; | ||
} | ||
return materials[mainIdentifier]; | ||
} | ||
// https://github.com/joliss/js-string-escape/blob/master/index.js | ||
@@ -759,38 +547,140 @@ // http://javascript.crockford.com/remedial.html | ||
var composition = decompose(value, { | ||
var _decompose = decompose(value, { | ||
functionAllowed: functionAllowed | ||
}), | ||
recipeArray = _decompose.recipeArray, | ||
mainIdentifier = _decompose.mainIdentifier, | ||
valueMap = _decompose.valueMap; | ||
var recipeArraySorted = sortRecipe(recipeArray); | ||
var source = "(function () {\nObject.defineProperty(Object.prototype, \"__global__\", {\n get: function () { return this },\n configurable: true,\n});\nvar globalObject = __global__;\ndelete Object.prototype.__global__;\n\nfunction safeDefineProperty(object, propertyNameOrSymbol, descriptor) {\n var currentDescriptor = Object.getOwnPropertyDescriptor(object, propertyNameOrSymbol);\n if (currentDescriptor && !currentDescriptor.configurable) return\n Object.defineProperty(object, propertyNameOrSymbol, descriptor)\n};\n"; | ||
var variableNameMap = {}; | ||
recipeArray.forEach(function (recipe, index) { | ||
var indexSorted = recipeArraySorted.indexOf(recipe); | ||
variableNameMap[index] = "_".concat(indexSorted); | ||
}); | ||
var recipe = compositionToRecipe(composition); | ||
var recipeMinified = minifyRecipe(recipe); | ||
var recomposeSource = recompose.toString(); | ||
var recomposeParamSouce = recipeToSource(recipeMinified); | ||
return "(".concat(recomposeSource, ")(").concat(recomposeParamSouce, ")"); | ||
}; | ||
var recipeToSource = function recipeToSource(recipe) { | ||
var valueToSource = function valueToSource(value) { | ||
if (value instanceof Array) return arrayToSource(value); | ||
if (value instanceof RegExp) return value.toString(); | ||
if (value === null) return "null"; | ||
if (_typeof(value) === "object") return objectToSource(value); | ||
if (typeof value === "string") return "\"".concat(escapeString(value), "\""); | ||
if (Object.is(value, -0)) return "-0"; | ||
return String(value); | ||
var identifierToVariableName = function identifierToVariableName(identifier) { | ||
return variableNameMap[identifier]; | ||
}; | ||
var arrayToSource = function arrayToSource(array) { | ||
var valueSourceArray = array.map(function (value) { | ||
return valueToSource(value); | ||
var recipeToSetupSource = function recipeToSetupSource(recipe) { | ||
if (recipe.type === "primitive") return primitiveRecipeToSetupSource(recipe); | ||
if (recipe.type === "global-symbol") return globalSymbolRecipeToSetupSource(recipe); | ||
if (recipe.type === "global-reference") return globalReferenceRecipeToSetupSource(recipe); | ||
return compositeRecipeToSetupSource(recipe); | ||
}; | ||
var primitiveRecipeToSetupSource = function primitiveRecipeToSetupSource(_ref2) { | ||
var value = _ref2.value; | ||
if (typeof value === "string") return "\"".concat(escapeString(value), "\";"); | ||
if (Object.is(value, -0)) return "-0;"; | ||
return "".concat(String(value), ";"); | ||
}; | ||
var globalSymbolRecipeToSetupSource = function globalSymbolRecipeToSetupSource(recipe) { | ||
return "Symbol.for(\"".concat(escapeString(recipe.key), "\");"); | ||
}; | ||
var globalReferenceRecipeToSetupSource = function globalReferenceRecipeToSetupSource(recipe) { | ||
var pathSource = recipe.path.map(function (part) { | ||
return "[\"".concat(escapeString(part), "\"]"); | ||
}).join(""); | ||
return "globalObject".concat(pathSource, ";"); | ||
}; | ||
var compositeRecipeToSetupSource = function compositeRecipeToSetupSource(_ref3) { | ||
var prototypeIdentifier = _ref3.prototypeIdentifier, | ||
valueOfIdentifier = _ref3.valueOfIdentifier; | ||
if (prototypeIdentifier === undefined) return identifierToVariableName(valueOfIdentifier); | ||
var prototypeValue = valueMap[prototypeIdentifier]; | ||
if (prototypeValue === null) return "Object.create(null);"; | ||
var prototypeConstructor = prototypeValue.constructor; | ||
if (prototypeConstructor === Object) return "Object.create(".concat(identifierToVariableName(prototypeIdentifier), ");"); | ||
if (valueOfIdentifier === undefined) return "new ".concat(prototypeConstructor.name, "();"); | ||
return "new ".concat(prototypeConstructor.name, "(").concat(identifierToVariableName(valueOfIdentifier), ");"); | ||
}; | ||
recipeArraySorted.forEach(function (recipe) { | ||
var recipeVariableName = identifierToVariableName(recipeArray.indexOf(recipe)); | ||
source += "var ".concat(recipeVariableName, " = ").concat(recipeToSetupSource(recipe, recipeVariableName), "\n"); | ||
}); | ||
var recipeToMutateSource = function recipeToMutateSource(recipe, recipeVariableName) { | ||
if (recipe.type === "composite") return compositeRecipeToMutateSource(recipe, recipeVariableName); | ||
return ""; | ||
}; | ||
var compositeRecipeToMutateSource = function compositeRecipeToMutateSource(_ref4, recipeVariableName) { | ||
var propertyDescriptionArray = _ref4.propertyDescriptionArray, | ||
symbolDescriptionArray = _ref4.symbolDescriptionArray, | ||
methodDescriptionArray = _ref4.methodDescriptionArray, | ||
extensible = _ref4.extensible; | ||
var mutateSource = ""; | ||
propertyDescriptionArray.forEach(function (_ref5) { | ||
var propertyNameIdentifier = _ref5.propertyNameIdentifier, | ||
propertyDescription = _ref5.propertyDescription; | ||
mutateSource += generateDefinePropertySource(recipeVariableName, propertyNameIdentifier, propertyDescription); | ||
}); | ||
return "[".concat(valueSourceArray.join(","), "]"); | ||
symbolDescriptionArray.forEach(function (_ref6) { | ||
var symbolIdentifier = _ref6.symbolIdentifier, | ||
propertyDescription = _ref6.propertyDescription; | ||
mutateSource += generateDefinePropertySource(recipeVariableName, symbolIdentifier, propertyDescription); | ||
}); | ||
methodDescriptionArray.forEach(function (_ref7) { | ||
var methodNameIdentifier = _ref7.methodNameIdentifier, | ||
callArray = _ref7.callArray; | ||
mutateSource += generateMethodCallSource(recipeVariableName, methodNameIdentifier, callArray); | ||
}); | ||
if (!extensible) { | ||
mutateSource += generatePreventExtensionSource(recipeVariableName); | ||
} | ||
return mutateSource; | ||
}; | ||
var objectToSource = function objectToSource(object) { | ||
var propertiesSources = Object.keys(object).map(function (key) { | ||
return "\"".concat(escapeString(key), "\":").concat(valueToSource(object[key])); | ||
var generateDefinePropertySource = function generateDefinePropertySource(recipeVariableName, propertyNameOrSymbolIdentifier, propertyDescription) { | ||
var propertyOrSymbolVariableName = identifierToVariableName(propertyNameOrSymbolIdentifier); | ||
var propertyDescriptorSource = generatePropertyDescriptorSource(propertyDescription); | ||
return "safeDefineProperty(".concat(recipeVariableName, ", ").concat(propertyOrSymbolVariableName, ", ").concat(propertyDescriptorSource, ");"); | ||
}; | ||
var generatePropertyDescriptorSource = function generatePropertyDescriptorSource(_ref8) { | ||
var configurable = _ref8.configurable, | ||
writable = _ref8.writable, | ||
enumerable = _ref8.enumerable, | ||
getIdentifier = _ref8.getIdentifier, | ||
setIdentifier = _ref8.setIdentifier, | ||
valueIdentifier = _ref8.valueIdentifier; | ||
if (valueIdentifier === undefined) { | ||
return "{\n configurable: ".concat(configurable, ",\n enumerable: ").concat(enumerable, ",\n get: ").concat(getIdentifier === undefined ? undefined : identifierToVariableName(getIdentifier), ",\n set: ").concat(setIdentifier === undefined ? undefined : identifierToVariableName(setIdentifier), ",\n}"); | ||
} | ||
return "{\n configurable: ".concat(configurable, ",\n writable: ").concat(writable, ",\n enumerable: ").concat(enumerable, ",\n value: ").concat(valueIdentifier === undefined ? undefined : identifierToVariableName(valueIdentifier), "\n}"); | ||
}; | ||
var generateMethodCallSource = function generateMethodCallSource(recipeVariableName, methodNameIdentifier, callArray) { | ||
var methodCallSource = ""; | ||
var methodVariableName = identifierToVariableName(methodNameIdentifier); | ||
callArray.forEach(function (argumentIdentifiers) { | ||
var argumentVariableNames = argumentIdentifiers.map(function (argumentIdentifier) { | ||
return identifierToVariableName(argumentIdentifier); | ||
}); | ||
methodCallSource += "".concat(recipeVariableName, "[").concat(methodVariableName, "](").concat(argumentVariableNames.join(","), ");"); | ||
}); | ||
return "{".concat(propertiesSources.join(","), "}"); | ||
return methodCallSource; | ||
}; | ||
return valueToSource(recipe); | ||
var generatePreventExtensionSource = function generatePreventExtensionSource(recipeVariableName) { | ||
return "Object.preventExtensions(".concat(recipeVariableName, ");"); | ||
}; | ||
recipeArraySorted.forEach(function (recipe) { | ||
var recipeVariableName = identifierToVariableName(recipeArray.indexOf(recipe)); | ||
source += "".concat(recipeToMutateSource(recipe, recipeVariableName)); | ||
}); | ||
source += "return ".concat(identifierToVariableName(mainIdentifier), "; })()"); | ||
return source; | ||
}; | ||
@@ -797,0 +687,0 @@ |
@@ -158,41 +158,28 @@ var __dmail_uneval__ = function (exports) { | ||
var propertiesDescription = {}; | ||
var propertyDescriptionArray = []; | ||
Object.getOwnPropertyNames(value).forEach(function (propertyName) { | ||
var propertyDescriptor = Object.getOwnPropertyDescriptor(value, propertyName); | ||
var propertyDescription = {}; | ||
Object.keys(propertyDescriptor).forEach(function (descriptorName) { | ||
var descriptorValue = propertyDescriptor[descriptorName]; | ||
if (descriptorName === "set" && descriptorValue && !functionAllowed) throw new Error(createForbiddenPropertySetterMessage({ | ||
path: path, | ||
propertyName: propertyName | ||
})); | ||
if (descriptorName === "get" && descriptorValue && !functionAllowed) throw new Error(createForbiddenPropertyGetterMessage({ | ||
path: path, | ||
propertyName: propertyName | ||
})); | ||
var descriptorNameIdentifier = valueToIdentifier(descriptorName, _toConsumableArray(path).concat(["[\"".concat(propertyName, "\"]"), "[[propertyDescriptor:".concat(descriptorName, "]]")])); | ||
var descriptorValueIdentifier = valueToIdentifier(descriptorValue, _toConsumableArray(path).concat(["[\"".concat(propertyName, "\"]"), "[[propertyDescriptor:".concat(descriptorName, "]]")])); | ||
propertyDescription[descriptorNameIdentifier] = descriptorValueIdentifier; | ||
var propertyNameIdentifier = valueToIdentifier(propertyName, _toConsumableArray(path).concat([propertyName])); | ||
var propertyDescription = computePropertyDescription(propertyDescriptor, propertyName, path); | ||
propertyDescriptionArray.push({ | ||
propertyNameIdentifier: propertyNameIdentifier, | ||
propertyDescription: propertyDescription | ||
}); | ||
var propertyNameIdentifier = valueToIdentifier(propertyName, _toConsumableArray(path).concat([propertyName])); | ||
propertiesDescription[propertyNameIdentifier] = propertyDescription; | ||
}); | ||
var symbolsDescription = {}; | ||
var symbolDescriptionArray = []; | ||
Object.getOwnPropertySymbols(value).forEach(function (symbol) { | ||
var propertyDescriptor = Object.getOwnPropertyDescriptor(value, symbol); | ||
var propertyDescription = {}; | ||
Object.keys(propertyDescriptor).forEach(function (descriptorName) { | ||
var descriptorNameIdentifier = valueToIdentifier(descriptorName, _toConsumableArray(path).concat(["[".concat(symbol.toString(), "]"), "[[propertyDescriptor:".concat(descriptorName, "]]")])); | ||
var descriptorValueIdentifier = valueToIdentifier(propertyDescriptor[descriptorName], _toConsumableArray(path).concat(["[".concat(symbol.toString(), "]"), "[[propertyDescriptor:".concat(descriptorName, "]]")])); | ||
propertyDescription[descriptorNameIdentifier] = descriptorValueIdentifier; | ||
var symbolIdentifier = valueToIdentifier(symbol, _toConsumableArray(path).concat(["[".concat(symbol.toString(), "]")])); | ||
var propertyDescription = computePropertyDescription(propertyDescriptor, symbol, path); | ||
symbolDescriptionArray.push({ | ||
symbolIdentifier: symbolIdentifier, | ||
propertyDescription: propertyDescription | ||
}); | ||
var symbolIdentifier = valueToIdentifier(symbol, _toConsumableArray(path).concat([symbol])); | ||
symbolsDescription[symbolIdentifier] = propertyDescription; | ||
}); | ||
var methodsDescription = computeMethodsDescription(value, path); | ||
var methodDescriptionArray = computeMethodDescriptionArray(value, path); | ||
var extensible = Object.isExtensible(value); | ||
recipeArray[identifier] = createCompositeRecipe({ | ||
propertiesDescription: propertiesDescription, | ||
symbolsDescription: symbolsDescription, | ||
methodsDescription: methodsDescription, | ||
propertyDescriptionArray: propertyDescriptionArray, | ||
symbolDescriptionArray: symbolDescriptionArray, | ||
methodDescriptionArray: methodDescriptionArray, | ||
extensible: extensible | ||
@@ -203,25 +190,51 @@ }); | ||
var computeMethodsDescription = function computeMethodsDescription(value, path) { | ||
var methodsDescription = {}; | ||
var computePropertyDescription = function computePropertyDescription(propertyDescriptor, propertyNameOrSymbol, path) { | ||
if (propertyDescriptor.set && !functionAllowed) throw new Error(createForbiddenPropertySetterMessage({ | ||
path: path, | ||
propertyNameOrSymbol: propertyNameOrSymbol | ||
})); | ||
if (propertyDescriptor.get && !functionAllowed) throw new Error(createForbiddenPropertyGetterMessage({ | ||
path: path, | ||
propertyNameOrSymbol: propertyNameOrSymbol | ||
})); | ||
return { | ||
configurable: propertyDescriptor.configurable, | ||
writable: propertyDescriptor.writable, | ||
enumerable: propertyDescriptor.enumerable, | ||
getIdentifier: "get" in propertyDescriptor ? valueToIdentifier(propertyDescriptor.get, _toConsumableArray(path).concat([String(propertyNameOrSymbol), "[[descriptor:get]]"])) : undefined, | ||
setIdentifier: "set" in propertyDescriptor ? valueToIdentifier(propertyDescriptor.set, _toConsumableArray(path).concat([String(propertyNameOrSymbol), "[[descriptor:set]]"])) : undefined, | ||
valueIdentifier: "value" in propertyDescriptor ? valueToIdentifier(propertyDescriptor.value, _toConsumableArray(path).concat([String(propertyNameOrSymbol), "[[descriptor:value]]"])) : undefined | ||
}; | ||
}; | ||
var computeMethodDescriptionArray = function computeMethodDescriptionArray(value, path) { | ||
var methodDescriptionArray = []; | ||
if (typeof Set === "function" && value instanceof Set) { | ||
var addCallArray = []; | ||
var callArray = []; | ||
value.forEach(function (entryValue, index) { | ||
var entryValueIdentifier = valueToIdentifier(entryValue, _toConsumableArray(path).concat(["[[SetEntryValue]]", index])); | ||
addCallArray.push([entryValueIdentifier]); | ||
callArray.push([entryValueIdentifier]); | ||
}); | ||
methodsDescription[valueToIdentifier("add")] = addCallArray; | ||
methodDescriptionArray.push({ | ||
methodNameIdentifier: valueToIdentifier("add"), | ||
callArray: callArray | ||
}); | ||
} | ||
if (typeof Map === "function" && value instanceof Map) { | ||
var setCallArray = []; | ||
var _callArray = []; | ||
value.forEach(function (entryValue, entryKey) { | ||
var entryKeyIdentifier = valueToIdentifier(entryKey, _toConsumableArray(path).concat(["[[MapEntryKey]]", entryKey])); | ||
var entryValueIdentifier = valueToIdentifier(entryValue, _toConsumableArray(path).concat(["[[MapEntryValue]]", entryValue])); | ||
setCallArray.push([entryKeyIdentifier, entryValueIdentifier]); | ||
_callArray.push([entryKeyIdentifier, entryValueIdentifier]); | ||
}); | ||
methodsDescription[valueToIdentifier("set")] = setCallArray; | ||
methodDescriptionArray.push({ | ||
methodNameIdentifier: valueToIdentifier("set"), | ||
callArray: _callArray | ||
}); | ||
} | ||
return methodsDescription; | ||
return methodDescriptionArray; | ||
}; | ||
@@ -327,3 +340,4 @@ | ||
recipeArray: recipeArray, | ||
mainIdentifier: mainIdentifier | ||
mainIdentifier: mainIdentifier, | ||
valueMap: valueMap | ||
}; | ||
@@ -372,5 +386,5 @@ }; | ||
valueOfIdentifier = _ref2.valueOfIdentifier, | ||
propertiesDescription = _ref2.propertiesDescription, | ||
symbolsDescription = _ref2.symbolsDescription, | ||
methodsDescription = _ref2.methodsDescription, | ||
propertyDescriptionArray = _ref2.propertyDescriptionArray, | ||
symbolDescriptionArray = _ref2.symbolDescriptionArray, | ||
methodDescriptionArray = _ref2.methodDescriptionArray, | ||
extensible = _ref2.extensible; | ||
@@ -381,5 +395,5 @@ return { | ||
valueOfIdentifier: valueOfIdentifier, | ||
propertiesDescription: propertiesDescription, | ||
symbolsDescription: symbolsDescription, | ||
methodsDescription: methodsDescription, | ||
propertyDescriptionArray: propertyDescriptionArray, | ||
symbolDescriptionArray: symbolDescriptionArray, | ||
methodDescriptionArray: methodDescriptionArray, | ||
extensible: extensible | ||
@@ -415,4 +429,4 @@ }; | ||
var path = _ref7.path, | ||
propertyName = _ref7.propertyName; | ||
return "property getter are not allowed.\ngetter found on property: ".concat(propertyName, "\nat: ").concat(path.join("")); | ||
propertyNameOrSymbol = _ref7.propertyNameOrSymbol; | ||
return "property getter are not allowed.\ngetter found on property: ".concat(String(propertyNameOrSymbol), "\nat: ").concat(path.join("")); | ||
}; | ||
@@ -422,4 +436,4 @@ | ||
var path = _ref8.path, | ||
propertyName = _ref8.propertyName; | ||
return "property setter are not allowed.\nsetter found on property: ".concat(propertyName, "\nat: ").concat(path.join("")); | ||
propertyNameOrSymbol = _ref8.propertyNameOrSymbol; | ||
return "property setter are not allowed.\nsetter found on property: ".concat(String(propertyNameOrSymbol), "\nat: ").concat(path.join("")); | ||
}; | ||
@@ -443,6 +457,3 @@ | ||
var compositionToRecipe = function compositionToRecipe(_ref) { | ||
var recipeArray = _ref.recipeArray, | ||
mainIdentifier = _ref.mainIdentifier; | ||
var sortRecipe = function sortRecipe(recipeArray) { | ||
var findInRecipePrototypeChain = function findInRecipePrototypeChain(recipe, callback) { | ||
@@ -494,227 +505,4 @@ var currentRecipe = recipe; // eslint-disable-next-line no-constant-condition | ||
}); | ||
var moves = {}; | ||
recipeArray.forEach(function (recipe, index) { | ||
var indexOrdered = recipeArrayOrdered.indexOf(recipe); | ||
if (index !== indexOrdered) { | ||
moves[index] = indexOrdered; | ||
} | ||
}); | ||
var remapIdentifier = function remapIdentifier(identifier) { | ||
if (identifier in moves) return moves[identifier]; | ||
return identifier; | ||
}; | ||
var remapPropertyDescription = function remapPropertyDescription(propertyDescription) { | ||
var remappedPropertyDescription = {}; | ||
Object.keys(propertyDescription).forEach(function (descriptorNameIdentifier) { | ||
remappedPropertyDescription[remapIdentifier(descriptorNameIdentifier)] = remapIdentifier(propertyDescription[descriptorNameIdentifier]); | ||
}); | ||
return remappedPropertyDescription; | ||
}; | ||
recipeArrayOrdered.forEach(function (recipe) { | ||
if (recipe.type !== "composite") return; | ||
recipe.valueOfIdentifier = remapIdentifier(recipe.valueOfIdentifier); | ||
recipe.prototypeIdentifier = remapIdentifier(recipe.prototypeIdentifier); | ||
var propertiesDescription = recipe.propertiesDescription; | ||
var remappedPropertiesDescription = {}; | ||
Object.keys(propertiesDescription).forEach(function (propertyNameIdentifier) { | ||
remappedPropertiesDescription[remapIdentifier(propertyNameIdentifier)] = remapPropertyDescription(propertiesDescription[propertyNameIdentifier]); | ||
}); | ||
recipe.propertiesDescription = remappedPropertiesDescription; | ||
var symbolsDescription = recipe.symbolsDescription; | ||
var remappedSymbolsDescription = {}; | ||
Object.keys(symbolsDescription).forEach(function (symbolIdentifier) { | ||
remappedSymbolsDescription[remapIdentifier(symbolIdentifier)] = remapPropertyDescription(symbolsDescription[symbolIdentifier]); | ||
}); | ||
recipe.symbolsDescription = remappedSymbolsDescription; | ||
var methodsDescription = recipe.methodsDescription; | ||
var remappedMethodsDescription = {}; | ||
Object.keys(methodsDescription).forEach(function (methodNameIdentifier) { | ||
var callsDescription = methodsDescription[methodNameIdentifier]; | ||
var remappedCallsDescription = callsDescription.map(function (argumentIdentifiers) { | ||
return argumentIdentifiers.map(function (argumentIdentifier) { | ||
return remapIdentifier(argumentIdentifier); | ||
}); | ||
}); | ||
remappedMethodsDescription[remapIdentifier(methodNameIdentifier)] = remappedCallsDescription; | ||
}); | ||
recipe.methodsDescription = remappedMethodsDescription; | ||
}); | ||
return { | ||
recipeArray: recipeArrayOrdered, | ||
mainIdentifier: remapIdentifier(mainIdentifier) | ||
}; | ||
}; // This function would return recipes that does not contain default values | ||
// for instance we'll consider extensible as false by default | ||
// empty propertiesMap and symbolsMap can be omitted | ||
// it would decrease the size of value string representation a bit | ||
var minifyRecipe = function minifyRecipe(_ref) { | ||
var recipeArray = _ref.recipeArray, | ||
mainIdentifier = _ref.mainIdentifier; | ||
recipeArray.forEach(function (recipe) { | ||
if (recipe.type === "composite") { | ||
if (recipe.prototypeIdentifier === undefined) delete recipe.prototypeIdentifier; | ||
if (recipe.valueOfIdentifier === undefined) delete recipe.valueOfIdentifier; | ||
if (recipe.extensible === true) delete recipe.extensible; | ||
if (Object.keys(recipe.propertiesDescription).length === 0) delete recipe.propertiesDescription; | ||
if (Object.keys(recipe.symbolsDescription).length === 0) delete recipe.symbolsDescription; | ||
if (Object.keys(recipe.methodsDescription).length === 0) delete recipe.methodsDescription; | ||
} | ||
}); | ||
return { | ||
recipeArray: recipeArray, | ||
mainIdentifier: mainIdentifier | ||
}; | ||
}; | ||
function recompose(param) { | ||
var recipeArray = param.recipeArray; | ||
var mainIdentifier = param.mainIdentifier; | ||
var materials = {}; // eslint-disable-next-line no-extend-native | ||
Object.defineProperty(Object.prototype, "__global__", { | ||
// eslint-disable-next-line object-shorthand | ||
get: function get() { | ||
return this; | ||
}, | ||
configurable: true | ||
}); // eslint-disable-next-line no-undef | ||
var globalObject = __global__; | ||
delete Object.prototype.__global__; | ||
function setupMaterial(recipe) { | ||
if (recipe.type === "primitive") return setupPrimitiveMaterial(recipe); | ||
if (recipe.type === "global-symbol") return setupGlobalSymbolMaterial(recipe); | ||
if (recipe.type === "global-reference") return setupGlobalReferenceMaterial(recipe); | ||
return setupCompositeMaterial(recipe); | ||
} | ||
function setupPrimitiveMaterial(recipe) { | ||
return recipe.value; | ||
} | ||
function setupGlobalSymbolMaterial(recipe) { | ||
return Symbol.for(recipe.key); | ||
} | ||
function setupGlobalReferenceMaterial(recipe) { | ||
var path = recipe.path; | ||
var currentValue = globalObject; | ||
var i = 0; | ||
while (i < path.length) { | ||
var part = path[i]; | ||
i++; | ||
if (part in currentValue === false) throw new Error(createValueNotFoundErrorMessage({ | ||
path: path, | ||
index: i | ||
})); | ||
currentValue = currentValue[part]; | ||
} | ||
return currentValue; | ||
} | ||
function setupCompositeMaterial(recipe) { | ||
var prototypeIdentifier = recipe.prototypeIdentifier; | ||
var valueOfIdentifier = recipe.valueOfIdentifier; // regexp and function | ||
if (prototypeIdentifier === undefined) return materials[valueOfIdentifier]; | ||
var prototypeValue = materials[prototypeIdentifier]; | ||
if (prototypeValue === null) return Object.create(null); | ||
var Constructor = prototypeValue.constructor; | ||
if (Constructor === Object) return Object.create(prototypeValue); | ||
if (valueOfIdentifier === undefined) return new Constructor(); | ||
var valueOfValue = materials[valueOfIdentifier]; | ||
return new Constructor(valueOfValue); | ||
} | ||
recipeArray.forEach(function (recipe, index) { | ||
var value = setupMaterial(recipe); | ||
materials[index] = value; | ||
}); | ||
function followRecipe(recipe, index) { | ||
if (recipe.type === "composite") followCompositeRecipe(recipe, index); | ||
} | ||
function followCompositeRecipe(recipe, index) { | ||
var composite = materials[index]; | ||
var propertiesDescription = recipe.propertiesDescription; | ||
if (propertiesDescription) { | ||
Object.keys(propertiesDescription).forEach(function (propertyNameIdentifier) { | ||
var propertyName = materials[propertyNameIdentifier]; | ||
var description = propertiesDescription[propertyNameIdentifier]; | ||
defineProperty(composite, propertyName, description); | ||
}); | ||
} | ||
var symbolsDescription = recipe.symbolsDescription; | ||
if (symbolsDescription) { | ||
Object.keys(symbolsDescription).forEach(function (symbolIdentifier) { | ||
var symbol = materials[symbolIdentifier]; | ||
var description = symbolsDescription[symbolIdentifier]; | ||
defineProperty(composite, symbol, description); | ||
}); | ||
} | ||
var methodsDescription = recipe.methodsDescription; | ||
if (methodsDescription) { | ||
Object.keys(methodsDescription).forEach(function (methodNameIdentifier) { | ||
var methodName = materials[methodNameIdentifier]; | ||
var calls = methodsDescription[methodNameIdentifier]; | ||
calls.forEach(function (argumentIdentifiers) { | ||
var argumentValues = argumentIdentifiers.map(function (argumentIdentifier) { | ||
return materials[argumentIdentifier]; | ||
}); | ||
composite[methodName].apply(composite, _toConsumableArray(argumentValues)); | ||
}); | ||
}); | ||
} | ||
var extensible = recipe.extensible || true; | ||
if (!extensible) Object.preventExtensions(composite); | ||
} | ||
function defineProperty(composite, propertyNameOrSymbol, propertyDescription) { | ||
var currentDescriptor = Object.getOwnPropertyDescriptor(composite, propertyNameOrSymbol); | ||
if (currentDescriptor && currentDescriptor.configurable === false) return; | ||
var descriptor = {}; | ||
Object.keys(propertyDescription).forEach(function (descriptorNameIdentifier) { | ||
var descriptorValueIdentifier = propertyDescription[descriptorNameIdentifier]; | ||
var descriptorName = materials[descriptorNameIdentifier]; | ||
var descriptorValue = materials[descriptorValueIdentifier]; | ||
descriptor[descriptorName] = descriptorValue; | ||
}); | ||
Object.defineProperty(composite, propertyNameOrSymbol, descriptor); | ||
} | ||
recipeArray.forEach(function (recipe, index) { | ||
followRecipe(recipe, index); | ||
}); | ||
function createValueNotFoundErrorMessage(data) { | ||
var path = data.path; | ||
var index = data.index; | ||
var message = "value not found for path."; | ||
message += "\n"; // eslint-disable-next-line prefer-template | ||
message += "part not found: " + path[index]; | ||
message += "\n"; // eslint-disable-next-line prefer-template | ||
message += "path: " + path.join(""); | ||
return message; | ||
} | ||
return materials[mainIdentifier]; | ||
} // https://github.com/joliss/js-string-escape/blob/master/index.js | ||
return recipeArrayOrdered; | ||
}; // https://github.com/joliss/js-string-escape/blob/master/index.js | ||
// http://javascript.crockford.com/remedial.html | ||
@@ -759,38 +547,140 @@ | ||
var composition = decompose(value, { | ||
var _decompose = decompose(value, { | ||
functionAllowed: functionAllowed | ||
}), | ||
recipeArray = _decompose.recipeArray, | ||
mainIdentifier = _decompose.mainIdentifier, | ||
valueMap = _decompose.valueMap; | ||
var recipeArraySorted = sortRecipe(recipeArray); | ||
var source = "(function () {\nObject.defineProperty(Object.prototype, \"__global__\", {\n get: function () { return this },\n configurable: true,\n});\nvar globalObject = __global__;\ndelete Object.prototype.__global__;\n\nfunction safeDefineProperty(object, propertyNameOrSymbol, descriptor) {\n var currentDescriptor = Object.getOwnPropertyDescriptor(object, propertyNameOrSymbol);\n if (currentDescriptor && !currentDescriptor.configurable) return\n Object.defineProperty(object, propertyNameOrSymbol, descriptor)\n};\n"; | ||
var variableNameMap = {}; | ||
recipeArray.forEach(function (recipe, index) { | ||
var indexSorted = recipeArraySorted.indexOf(recipe); | ||
variableNameMap[index] = "_".concat(indexSorted); | ||
}); | ||
var recipe = compositionToRecipe(composition); | ||
var recipeMinified = minifyRecipe(recipe); | ||
var recomposeSource = recompose.toString(); | ||
var recomposeParamSouce = recipeToSource(recipeMinified); | ||
return "(".concat(recomposeSource, ")(").concat(recomposeParamSouce, ")"); | ||
}; | ||
var recipeToSource = function recipeToSource(recipe) { | ||
var valueToSource = function valueToSource(value) { | ||
if (value instanceof Array) return arrayToSource(value); | ||
if (value instanceof RegExp) return value.toString(); | ||
if (value === null) return "null"; | ||
if (_typeof(value) === "object") return objectToSource(value); | ||
if (typeof value === "string") return "\"".concat(escapeString(value), "\""); | ||
if (Object.is(value, -0)) return "-0"; | ||
return String(value); | ||
var identifierToVariableName = function identifierToVariableName(identifier) { | ||
return variableNameMap[identifier]; | ||
}; | ||
var arrayToSource = function arrayToSource(array) { | ||
var valueSourceArray = array.map(function (value) { | ||
return valueToSource(value); | ||
var recipeToSetupSource = function recipeToSetupSource(recipe) { | ||
if (recipe.type === "primitive") return primitiveRecipeToSetupSource(recipe); | ||
if (recipe.type === "global-symbol") return globalSymbolRecipeToSetupSource(recipe); | ||
if (recipe.type === "global-reference") return globalReferenceRecipeToSetupSource(recipe); | ||
return compositeRecipeToSetupSource(recipe); | ||
}; | ||
var primitiveRecipeToSetupSource = function primitiveRecipeToSetupSource(_ref2) { | ||
var value = _ref2.value; | ||
if (typeof value === "string") return "\"".concat(escapeString(value), "\";"); | ||
if (Object.is(value, -0)) return "-0;"; | ||
return "".concat(String(value), ";"); | ||
}; | ||
var globalSymbolRecipeToSetupSource = function globalSymbolRecipeToSetupSource(recipe) { | ||
return "Symbol.for(\"".concat(escapeString(recipe.key), "\");"); | ||
}; | ||
var globalReferenceRecipeToSetupSource = function globalReferenceRecipeToSetupSource(recipe) { | ||
var pathSource = recipe.path.map(function (part) { | ||
return "[\"".concat(escapeString(part), "\"]"); | ||
}).join(""); | ||
return "globalObject".concat(pathSource, ";"); | ||
}; | ||
var compositeRecipeToSetupSource = function compositeRecipeToSetupSource(_ref3) { | ||
var prototypeIdentifier = _ref3.prototypeIdentifier, | ||
valueOfIdentifier = _ref3.valueOfIdentifier; | ||
if (prototypeIdentifier === undefined) return identifierToVariableName(valueOfIdentifier); | ||
var prototypeValue = valueMap[prototypeIdentifier]; | ||
if (prototypeValue === null) return "Object.create(null);"; | ||
var prototypeConstructor = prototypeValue.constructor; | ||
if (prototypeConstructor === Object) return "Object.create(".concat(identifierToVariableName(prototypeIdentifier), ");"); | ||
if (valueOfIdentifier === undefined) return "new ".concat(prototypeConstructor.name, "();"); | ||
return "new ".concat(prototypeConstructor.name, "(").concat(identifierToVariableName(valueOfIdentifier), ");"); | ||
}; | ||
recipeArraySorted.forEach(function (recipe) { | ||
var recipeVariableName = identifierToVariableName(recipeArray.indexOf(recipe)); | ||
source += "var ".concat(recipeVariableName, " = ").concat(recipeToSetupSource(recipe, recipeVariableName), "\n"); | ||
}); | ||
var recipeToMutateSource = function recipeToMutateSource(recipe, recipeVariableName) { | ||
if (recipe.type === "composite") return compositeRecipeToMutateSource(recipe, recipeVariableName); | ||
return ""; | ||
}; | ||
var compositeRecipeToMutateSource = function compositeRecipeToMutateSource(_ref4, recipeVariableName) { | ||
var propertyDescriptionArray = _ref4.propertyDescriptionArray, | ||
symbolDescriptionArray = _ref4.symbolDescriptionArray, | ||
methodDescriptionArray = _ref4.methodDescriptionArray, | ||
extensible = _ref4.extensible; | ||
var mutateSource = ""; | ||
propertyDescriptionArray.forEach(function (_ref5) { | ||
var propertyNameIdentifier = _ref5.propertyNameIdentifier, | ||
propertyDescription = _ref5.propertyDescription; | ||
mutateSource += generateDefinePropertySource(recipeVariableName, propertyNameIdentifier, propertyDescription); | ||
}); | ||
return "[".concat(valueSourceArray.join(","), "]"); | ||
symbolDescriptionArray.forEach(function (_ref6) { | ||
var symbolIdentifier = _ref6.symbolIdentifier, | ||
propertyDescription = _ref6.propertyDescription; | ||
mutateSource += generateDefinePropertySource(recipeVariableName, symbolIdentifier, propertyDescription); | ||
}); | ||
methodDescriptionArray.forEach(function (_ref7) { | ||
var methodNameIdentifier = _ref7.methodNameIdentifier, | ||
callArray = _ref7.callArray; | ||
mutateSource += generateMethodCallSource(recipeVariableName, methodNameIdentifier, callArray); | ||
}); | ||
if (!extensible) { | ||
mutateSource += generatePreventExtensionSource(recipeVariableName); | ||
} | ||
return mutateSource; | ||
}; | ||
var objectToSource = function objectToSource(object) { | ||
var propertiesSources = Object.keys(object).map(function (key) { | ||
return "\"".concat(escapeString(key), "\":").concat(valueToSource(object[key])); | ||
var generateDefinePropertySource = function generateDefinePropertySource(recipeVariableName, propertyNameOrSymbolIdentifier, propertyDescription) { | ||
var propertyOrSymbolVariableName = identifierToVariableName(propertyNameOrSymbolIdentifier); | ||
var propertyDescriptorSource = generatePropertyDescriptorSource(propertyDescription); | ||
return "safeDefineProperty(".concat(recipeVariableName, ", ").concat(propertyOrSymbolVariableName, ", ").concat(propertyDescriptorSource, ");"); | ||
}; | ||
var generatePropertyDescriptorSource = function generatePropertyDescriptorSource(_ref8) { | ||
var configurable = _ref8.configurable, | ||
writable = _ref8.writable, | ||
enumerable = _ref8.enumerable, | ||
getIdentifier = _ref8.getIdentifier, | ||
setIdentifier = _ref8.setIdentifier, | ||
valueIdentifier = _ref8.valueIdentifier; | ||
if (valueIdentifier === undefined) { | ||
return "{\n configurable: ".concat(configurable, ",\n enumerable: ").concat(enumerable, ",\n get: ").concat(getIdentifier === undefined ? undefined : identifierToVariableName(getIdentifier), ",\n set: ").concat(setIdentifier === undefined ? undefined : identifierToVariableName(setIdentifier), ",\n}"); | ||
} | ||
return "{\n configurable: ".concat(configurable, ",\n writable: ").concat(writable, ",\n enumerable: ").concat(enumerable, ",\n value: ").concat(valueIdentifier === undefined ? undefined : identifierToVariableName(valueIdentifier), "\n}"); | ||
}; | ||
var generateMethodCallSource = function generateMethodCallSource(recipeVariableName, methodNameIdentifier, callArray) { | ||
var methodCallSource = ""; | ||
var methodVariableName = identifierToVariableName(methodNameIdentifier); | ||
callArray.forEach(function (argumentIdentifiers) { | ||
var argumentVariableNames = argumentIdentifiers.map(function (argumentIdentifier) { | ||
return identifierToVariableName(argumentIdentifier); | ||
}); | ||
methodCallSource += "".concat(recipeVariableName, "[").concat(methodVariableName, "](").concat(argumentVariableNames.join(","), ");"); | ||
}); | ||
return "{".concat(propertiesSources.join(","), "}"); | ||
return methodCallSource; | ||
}; | ||
return valueToSource(recipe); | ||
var generatePreventExtensionSource = function generatePreventExtensionSource(recipeVariableName) { | ||
return "Object.preventExtensions(".concat(recipeVariableName, ");"); | ||
}; | ||
recipeArraySorted.forEach(function (recipe) { | ||
var recipeVariableName = identifierToVariableName(recipeArray.indexOf(recipe)); | ||
source += "".concat(recipeToMutateSource(recipe, recipeVariableName)); | ||
}); | ||
source += "return ".concat(identifierToVariableName(mainIdentifier), "; })()"); | ||
return source; | ||
}; | ||
@@ -797,0 +687,0 @@ |
{ | ||
"name": "@dmail/uneval", | ||
"version": "5.3.0", | ||
"version": "5.4.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -76,6 +76,34 @@ # uneval | ||
## Example | ||
## How to use | ||
Live browser example: https://dmail.github.io/uneval/#browser-example. | ||
```shell | ||
npm install --save-dev @dmail/uneval | ||
``` | ||
Live node example: https://dmail.github.io/uneval/#node-example | ||
See browser and node examples below. | ||
### browser example | ||
```html | ||
<script src="https://unpkg.com/@dmail/uneval@5.3.0/dist/global/main.js"></script> | ||
<script> | ||
const { uneval } = window.__dmail_uneval__ | ||
console.log(eval(uneval({ answer: 42 }))) | ||
</script> | ||
``` | ||
### node example | ||
```js | ||
const { uneval } = require("@dmail/uneval") | ||
console.log(eval(uneval({ answer: 42 }))) | ||
``` | ||
## Interactive browser example | ||
— see https://dmail.github.io/uneval/browser-example. | ||
## Interactive node example | ||
— see https://dmail.github.io/uneval/node-example |
@@ -41,52 +41,19 @@ /** | ||
const propertiesDescription = {} | ||
const propertyDescriptionArray = [] | ||
Object.getOwnPropertyNames(value).forEach((propertyName) => { | ||
const propertyDescriptor = Object.getOwnPropertyDescriptor(value, propertyName) | ||
const propertyDescription = {} | ||
Object.keys(propertyDescriptor).forEach((descriptorName) => { | ||
const descriptorValue = propertyDescriptor[descriptorName] | ||
if (descriptorName === "set" && descriptorValue && !functionAllowed) | ||
throw new Error(createForbiddenPropertySetterMessage({ path, propertyName })) | ||
if (descriptorName === "get" && descriptorValue && !functionAllowed) | ||
throw new Error(createForbiddenPropertyGetterMessage({ path, propertyName })) | ||
const descriptorNameIdentifier = valueToIdentifier(descriptorName, [ | ||
...path, | ||
`["${propertyName}"]`, | ||
`[[propertyDescriptor:${descriptorName}]]`, | ||
]) | ||
const descriptorValueIdentifier = valueToIdentifier(descriptorValue, [ | ||
...path, | ||
`["${propertyName}"]`, | ||
`[[propertyDescriptor:${descriptorName}]]`, | ||
]) | ||
propertyDescription[descriptorNameIdentifier] = descriptorValueIdentifier | ||
}) | ||
const propertyNameIdentifier = valueToIdentifier(propertyName, [...path, propertyName]) | ||
propertiesDescription[propertyNameIdentifier] = propertyDescription | ||
const propertyDescription = computePropertyDescription(propertyDescriptor, propertyName, path) | ||
propertyDescriptionArray.push({ propertyNameIdentifier, propertyDescription }) | ||
}) | ||
const symbolsDescription = {} | ||
const symbolDescriptionArray = [] | ||
Object.getOwnPropertySymbols(value).forEach((symbol) => { | ||
const propertyDescriptor = Object.getOwnPropertyDescriptor(value, symbol) | ||
const propertyDescription = {} | ||
Object.keys(propertyDescriptor).forEach((descriptorName) => { | ||
const descriptorNameIdentifier = valueToIdentifier(descriptorName, [ | ||
...path, | ||
`[${symbol.toString()}]`, | ||
`[[propertyDescriptor:${descriptorName}]]`, | ||
]) | ||
const descriptorValueIdentifier = valueToIdentifier(propertyDescriptor[descriptorName], [ | ||
...path, | ||
`[${symbol.toString()}]`, | ||
`[[propertyDescriptor:${descriptorName}]]`, | ||
]) | ||
propertyDescription[descriptorNameIdentifier] = descriptorValueIdentifier | ||
}) | ||
const symbolIdentifier = valueToIdentifier(symbol, [...path, symbol]) | ||
symbolsDescription[symbolIdentifier] = propertyDescription | ||
const symbolIdentifier = valueToIdentifier(symbol, [...path, `[${symbol.toString()}]`]) | ||
const propertyDescription = computePropertyDescription(propertyDescriptor, symbol, path) | ||
symbolDescriptionArray.push({ symbolIdentifier, propertyDescription }) | ||
}) | ||
const methodsDescription = computeMethodsDescription(value, path) | ||
const methodDescriptionArray = computeMethodDescriptionArray(value, path) | ||
@@ -96,5 +63,5 @@ const extensible = Object.isExtensible(value) | ||
recipeArray[identifier] = createCompositeRecipe({ | ||
propertiesDescription, | ||
symbolsDescription, | ||
methodsDescription, | ||
propertyDescriptionArray, | ||
symbolDescriptionArray, | ||
methodDescriptionArray, | ||
extensible, | ||
@@ -105,7 +72,44 @@ }) | ||
const computeMethodsDescription = (value, path) => { | ||
const methodsDescription = {} | ||
const computePropertyDescription = (propertyDescriptor, propertyNameOrSymbol, path) => { | ||
if (propertyDescriptor.set && !functionAllowed) | ||
throw new Error(createForbiddenPropertySetterMessage({ path, propertyNameOrSymbol })) | ||
if (propertyDescriptor.get && !functionAllowed) | ||
throw new Error(createForbiddenPropertyGetterMessage({ path, propertyNameOrSymbol })) | ||
return { | ||
configurable: propertyDescriptor.configurable, | ||
writable: propertyDescriptor.writable, | ||
enumerable: propertyDescriptor.enumerable, | ||
getIdentifier: | ||
"get" in propertyDescriptor | ||
? valueToIdentifier(propertyDescriptor.get, [ | ||
...path, | ||
String(propertyNameOrSymbol), | ||
"[[descriptor:get]]", | ||
]) | ||
: undefined, | ||
setIdentifier: | ||
"set" in propertyDescriptor | ||
? valueToIdentifier(propertyDescriptor.set, [ | ||
...path, | ||
String(propertyNameOrSymbol), | ||
"[[descriptor:set]]", | ||
]) | ||
: undefined, | ||
valueIdentifier: | ||
"value" in propertyDescriptor | ||
? valueToIdentifier(propertyDescriptor.value, [ | ||
...path, | ||
String(propertyNameOrSymbol), | ||
"[[descriptor:value]]", | ||
]) | ||
: undefined, | ||
} | ||
} | ||
const computeMethodDescriptionArray = (value, path) => { | ||
const methodDescriptionArray = [] | ||
if (typeof Set === "function" && value instanceof Set) { | ||
const addCallArray = [] | ||
const callArray = [] | ||
value.forEach((entryValue, index) => { | ||
@@ -117,9 +121,9 @@ const entryValueIdentifier = valueToIdentifier(entryValue, [ | ||
]) | ||
addCallArray.push([entryValueIdentifier]) | ||
callArray.push([entryValueIdentifier]) | ||
}) | ||
methodsDescription[valueToIdentifier("add")] = addCallArray | ||
methodDescriptionArray.push({ methodNameIdentifier: valueToIdentifier("add"), callArray }) | ||
} | ||
if (typeof Map === "function" && value instanceof Map) { | ||
const setCallArray = [] | ||
const callArray = [] | ||
value.forEach((entryValue, entryKey) => { | ||
@@ -136,8 +140,8 @@ const entryKeyIdentifier = valueToIdentifier(entryKey, [ | ||
]) | ||
setCallArray.push([entryKeyIdentifier, entryValueIdentifier]) | ||
callArray.push([entryKeyIdentifier, entryValueIdentifier]) | ||
}) | ||
methodsDescription[valueToIdentifier("set")] = setCallArray | ||
methodDescriptionArray.push({ methodNameIdentifier: valueToIdentifier("set"), callArray }) | ||
} | ||
return methodsDescription | ||
return methodDescriptionArray | ||
} | ||
@@ -248,2 +252,3 @@ | ||
mainIdentifier, | ||
valueMap, | ||
} | ||
@@ -293,5 +298,5 @@ } | ||
valueOfIdentifier, | ||
propertiesDescription, | ||
symbolsDescription, | ||
methodsDescription, | ||
propertyDescriptionArray, | ||
symbolDescriptionArray, | ||
methodDescriptionArray, | ||
extensible, | ||
@@ -303,5 +308,5 @@ }) => { | ||
valueOfIdentifier, | ||
propertiesDescription, | ||
symbolsDescription, | ||
methodsDescription, | ||
propertyDescriptionArray, | ||
symbolDescriptionArray, | ||
methodDescriptionArray, | ||
extensible, | ||
@@ -341,5 +346,5 @@ } | ||
path, | ||
propertyName, | ||
propertyNameOrSymbol, | ||
}) => `property getter are not allowed. | ||
getter found on property: ${propertyName} | ||
getter found on property: ${String(propertyNameOrSymbol)} | ||
at: ${path.join("")}` | ||
@@ -349,5 +354,5 @@ | ||
path, | ||
propertyName, | ||
propertyNameOrSymbol, | ||
}) => `property setter are not allowed. | ||
setter found on property: ${propertyName} | ||
setter found on property: ${String(propertyNameOrSymbol)} | ||
at: ${path.join("")}` | ||
@@ -354,0 +359,0 @@ |
import { decompose } from "./decompose.js" | ||
import { compositionToRecipe } from "./composition-to-recipe.js" | ||
import { minifyRecipe } from "./minify-recipe.js" | ||
import { recompose } from "./recompose.js" | ||
import { sortRecipe } from "./sort-recipe.js" | ||
import { escapeString } from "./escapeString.js" | ||
export const uneval = (value, { functionAllowed = false } = {}) => { | ||
const composition = decompose(value, { functionAllowed }) | ||
const recipe = compositionToRecipe(composition) | ||
const recipeMinified = minifyRecipe(recipe) | ||
const recomposeSource = recompose.toString() | ||
const recomposeParamSouce = recipeToSource(recipeMinified) | ||
const { recipeArray, mainIdentifier, valueMap } = decompose(value, { functionAllowed }) | ||
const recipeArraySorted = sortRecipe(recipeArray) | ||
return `(${recomposeSource})(${recomposeParamSouce})` | ||
} | ||
let source = `(function () { | ||
Object.defineProperty(Object.prototype, "__global__", { | ||
get: function () { return this }, | ||
configurable: true, | ||
}); | ||
var globalObject = __global__; | ||
delete Object.prototype.__global__; | ||
const recipeToSource = (recipe) => { | ||
const valueToSource = (value) => { | ||
if (value instanceof Array) return arrayToSource(value) | ||
if (value instanceof RegExp) return value.toString() | ||
if (value === null) return "null" | ||
if (typeof value === "object") return objectToSource(value) | ||
if (typeof value === "string") return `"${escapeString(value)}"` | ||
if (Object.is(value, -0)) return "-0" | ||
return String(value) | ||
function safeDefineProperty(object, propertyNameOrSymbol, descriptor) { | ||
var currentDescriptor = Object.getOwnPropertyDescriptor(object, propertyNameOrSymbol); | ||
if (currentDescriptor && !currentDescriptor.configurable) return | ||
Object.defineProperty(object, propertyNameOrSymbol, descriptor) | ||
}; | ||
` | ||
const variableNameMap = {} | ||
recipeArray.forEach((recipe, index) => { | ||
const indexSorted = recipeArraySorted.indexOf(recipe) | ||
variableNameMap[index] = `_${indexSorted}` | ||
}) | ||
const identifierToVariableName = (identifier) => variableNameMap[identifier] | ||
const recipeToSetupSource = (recipe) => { | ||
if (recipe.type === "primitive") return primitiveRecipeToSetupSource(recipe) | ||
if (recipe.type === "global-symbol") return globalSymbolRecipeToSetupSource(recipe) | ||
if (recipe.type === "global-reference") return globalReferenceRecipeToSetupSource(recipe) | ||
return compositeRecipeToSetupSource(recipe) | ||
} | ||
const arrayToSource = (array) => { | ||
const valueSourceArray = array.map((value) => valueToSource(value)) | ||
return `[${valueSourceArray.join(",")}]` | ||
const primitiveRecipeToSetupSource = ({ value }) => { | ||
if (typeof value === "string") return `"${escapeString(value)}";` | ||
if (Object.is(value, -0)) return "-0;" | ||
return `${String(value)};` | ||
} | ||
const objectToSource = (object) => { | ||
const propertiesSources = Object.keys(object).map((key) => { | ||
return `"${escapeString(key)}":${valueToSource(object[key])}` | ||
const globalSymbolRecipeToSetupSource = (recipe) => { | ||
return `Symbol.for("${escapeString(recipe.key)}");` | ||
} | ||
const globalReferenceRecipeToSetupSource = (recipe) => { | ||
const pathSource = recipe.path.map((part) => `["${escapeString(part)}"]`).join("") | ||
return `globalObject${pathSource};` | ||
} | ||
const compositeRecipeToSetupSource = ({ prototypeIdentifier, valueOfIdentifier }) => { | ||
if (prototypeIdentifier === undefined) return identifierToVariableName(valueOfIdentifier) | ||
const prototypeValue = valueMap[prototypeIdentifier] | ||
if (prototypeValue === null) return `Object.create(null);` | ||
const prototypeConstructor = prototypeValue.constructor | ||
if (prototypeConstructor === Object) | ||
return `Object.create(${identifierToVariableName(prototypeIdentifier)});` | ||
if (valueOfIdentifier === undefined) return `new ${prototypeConstructor.name}();` | ||
return `new ${prototypeConstructor.name}(${identifierToVariableName(valueOfIdentifier)});` | ||
} | ||
recipeArraySorted.forEach((recipe) => { | ||
const recipeVariableName = identifierToVariableName(recipeArray.indexOf(recipe)) | ||
source += `var ${recipeVariableName} = ${recipeToSetupSource(recipe, recipeVariableName)} | ||
` | ||
}) | ||
const recipeToMutateSource = (recipe, recipeVariableName) => { | ||
if (recipe.type === "composite") | ||
return compositeRecipeToMutateSource(recipe, recipeVariableName) | ||
return `` | ||
} | ||
const compositeRecipeToMutateSource = ( | ||
{ propertyDescriptionArray, symbolDescriptionArray, methodDescriptionArray, extensible }, | ||
recipeVariableName, | ||
) => { | ||
let mutateSource = `` | ||
propertyDescriptionArray.forEach(({ propertyNameIdentifier, propertyDescription }) => { | ||
mutateSource += generateDefinePropertySource( | ||
recipeVariableName, | ||
propertyNameIdentifier, | ||
propertyDescription, | ||
) | ||
}) | ||
return `{${propertiesSources.join(",")}}` | ||
symbolDescriptionArray.forEach(({ symbolIdentifier, propertyDescription }) => { | ||
mutateSource += generateDefinePropertySource( | ||
recipeVariableName, | ||
symbolIdentifier, | ||
propertyDescription, | ||
) | ||
}) | ||
methodDescriptionArray.forEach(({ methodNameIdentifier, callArray }) => { | ||
mutateSource += generateMethodCallSource(recipeVariableName, methodNameIdentifier, callArray) | ||
}) | ||
if (!extensible) { | ||
mutateSource += generatePreventExtensionSource(recipeVariableName) | ||
} | ||
return mutateSource | ||
} | ||
return valueToSource(recipe) | ||
const generateDefinePropertySource = ( | ||
recipeVariableName, | ||
propertyNameOrSymbolIdentifier, | ||
propertyDescription, | ||
) => { | ||
const propertyOrSymbolVariableName = identifierToVariableName(propertyNameOrSymbolIdentifier) | ||
const propertyDescriptorSource = generatePropertyDescriptorSource(propertyDescription) | ||
return `safeDefineProperty(${recipeVariableName}, ${propertyOrSymbolVariableName}, ${propertyDescriptorSource});` | ||
} | ||
const generatePropertyDescriptorSource = ({ | ||
configurable, | ||
writable, | ||
enumerable, | ||
getIdentifier, | ||
setIdentifier, | ||
valueIdentifier, | ||
}) => { | ||
if (valueIdentifier === undefined) { | ||
return `{ | ||
configurable: ${configurable}, | ||
enumerable: ${enumerable}, | ||
get: ${getIdentifier === undefined ? undefined : identifierToVariableName(getIdentifier)}, | ||
set: ${setIdentifier === undefined ? undefined : identifierToVariableName(setIdentifier)}, | ||
}` | ||
} | ||
return `{ | ||
configurable: ${configurable}, | ||
writable: ${writable}, | ||
enumerable: ${enumerable}, | ||
value: ${valueIdentifier === undefined ? undefined : identifierToVariableName(valueIdentifier)} | ||
}` | ||
} | ||
const generateMethodCallSource = (recipeVariableName, methodNameIdentifier, callArray) => { | ||
let methodCallSource = `` | ||
const methodVariableName = identifierToVariableName(methodNameIdentifier) | ||
callArray.forEach((argumentIdentifiers) => { | ||
const argumentVariableNames = argumentIdentifiers.map((argumentIdentifier) => | ||
identifierToVariableName(argumentIdentifier), | ||
) | ||
methodCallSource += `${recipeVariableName}[${methodVariableName}](${argumentVariableNames.join( | ||
",", | ||
)});` | ||
}) | ||
return methodCallSource | ||
} | ||
const generatePreventExtensionSource = (recipeVariableName) => { | ||
return `Object.preventExtensions(${recipeVariableName});` | ||
} | ||
recipeArraySorted.forEach((recipe) => { | ||
const recipeVariableName = identifierToVariableName(recipeArray.indexOf(recipe)) | ||
source += `${recipeToMutateSource(recipe, recipeVariableName)}` | ||
}) | ||
source += `return ${identifierToVariableName(mainIdentifier)}; })()` | ||
return source | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
109
137620
14
1699