161 lines
9.8 KiB
JavaScript
161 lines
9.8 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.Logger = undefined;
|
|
|
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /**
|
|
* Copyright (c) 2017-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
var _realm = require("../realm.js");
|
|
|
|
var _errors = require("../errors.js");
|
|
|
|
var _index = require("../methods/index.js");
|
|
|
|
var _completions = require("../completions.js");
|
|
|
|
var _index2 = require("../values/index.js");
|
|
|
|
var _singletons = require("../singletons.js");
|
|
|
|
var _invariant = require("../invariant.js");
|
|
|
|
var _invariant2 = _interopRequireDefault(_invariant);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
var Logger = exports.Logger = function () {
|
|
function Logger(realm, internalDebug) {
|
|
_classCallCheck(this, Logger);
|
|
|
|
this.realm = realm;
|
|
this._hasErrors = false;
|
|
this.internalDebug = internalDebug;
|
|
}
|
|
|
|
_createClass(Logger, [{
|
|
key: "tryQuery",
|
|
|
|
|
|
// Wraps a query that might potentially execute user code.
|
|
value: function tryQuery(f, defaultValue, logFailures) {
|
|
var _this = this;
|
|
|
|
var realm = this.realm;
|
|
var context = new _realm.ExecutionContext();
|
|
context.isStrict = realm.isStrict;
|
|
var env = realm.$GlobalEnv;
|
|
context.lexicalEnvironment = env;
|
|
context.variableEnvironment = env;
|
|
context.realm = realm;
|
|
realm.pushContext(context);
|
|
// We use partial evaluation so that we can throw away any state mutations
|
|
var oldErrorHandler = realm.errorHandler;
|
|
var _newErrorHandler = void 0;
|
|
realm.errorHandler = _newErrorHandler = function newErrorHandler(d) {
|
|
if (d.severity === "Information" || d.severity === "Warning") return "Recover";
|
|
if (logFailures) {
|
|
realm.errorHandler = oldErrorHandler;
|
|
realm.handleError(d);
|
|
realm.errorHandler = _newErrorHandler;
|
|
}
|
|
return "Fail";
|
|
};
|
|
try {
|
|
var result = void 0;
|
|
var effects = realm.evaluateForEffects(function () {
|
|
try {
|
|
result = f();
|
|
} catch (e) {
|
|
if (e instanceof _completions.Completion) {
|
|
if (logFailures) _this.logCompletion(e);
|
|
result = defaultValue;
|
|
} else if (e instanceof _errors.FatalError) {
|
|
result = defaultValue;
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
return realm.intrinsics.undefined;
|
|
});
|
|
(0, _invariant2.default)(effects[0] === realm.intrinsics.undefined);
|
|
return result;
|
|
} finally {
|
|
realm.errorHandler = oldErrorHandler;
|
|
realm.popContext(context);
|
|
}
|
|
}
|
|
}, {
|
|
key: "logCompletion",
|
|
value: function logCompletion(res) {
|
|
var realm = this.realm;
|
|
var value = res.value;
|
|
if (this.internalDebug) console.error("=== " + res.constructor.name + " ===");
|
|
if (this.tryQuery(function () {
|
|
return value instanceof _index2.ObjectValue && (0, _index.InstanceofOperator)(realm, value, realm.intrinsics.Error);
|
|
}, false, false)) {
|
|
var object = value;
|
|
try {
|
|
var err = new _errors.FatalError(this.tryQuery(function () {
|
|
return _singletons.To.ToStringPartial(realm, (0, _index.Get)(realm, object, "message"));
|
|
}, "(unknown message)", false));
|
|
err.stack = this.tryQuery(function () {
|
|
return _singletons.To.ToStringPartial(realm, (0, _index.Get)(realm, object, "stack"));
|
|
}, "(unknown stack)", false);
|
|
console.error(err.message);
|
|
console.error(err.stack);
|
|
if (this.internalDebug && res instanceof _completions.ThrowCompletion) console.error(res.nativeStack);
|
|
} catch (err) {
|
|
var message = object.properties.get("message");
|
|
console.error(message && message.descriptor && message.descriptor.value instanceof _index2.StringValue ? message.descriptor.value.value : "(no message available)");
|
|
console.error(err.stack);
|
|
if (object.$ErrorData) {
|
|
console.error(object.$ErrorData.contextStack);
|
|
}
|
|
}
|
|
} else {
|
|
try {
|
|
value = _singletons.To.ToStringPartial(realm, value);
|
|
} catch (err) {
|
|
value = err.message;
|
|
}
|
|
console.error(value);
|
|
if (this.internalDebug && res instanceof _completions.ThrowCompletion) console.error(res.nativeStack);
|
|
}
|
|
this._hasErrors = true;
|
|
}
|
|
}, {
|
|
key: "logError",
|
|
value: function logError(value, message) {
|
|
var loc = value.expressionLocation;
|
|
if (loc) {
|
|
var locString = loc.start.line + ":" + (loc.start.column + 1);
|
|
if (loc.source) locString = loc.source + ":" + locString;
|
|
message = message + "\nat: " + locString;
|
|
} else if (value.intrinsicName) {
|
|
message = message + "\nintrinsic name: " + value.intrinsicName;
|
|
}
|
|
|
|
console.error(message);
|
|
this._hasErrors = true;
|
|
}
|
|
}, {
|
|
key: "hasErrors",
|
|
value: function hasErrors() {
|
|
return this._hasErrors;
|
|
}
|
|
}]);
|
|
|
|
return Logger;
|
|
}();
|
|
//# sourceMappingURL=logger.js.map
|