UNPKG

prepack

Version:

Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.

103 lines (88 loc) 4.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = function (realm) { let intrinsicName = 'process.binding("fs")'; let nativeFS = process.binding("fs"); // fs let obj = new _index.ObjectValue(realm, realm.intrinsics.ObjectPrototype, intrinsicName); obj.defineNativeMethod("FSInitialize", 0, (context, args) => { // TODO: Implement the native implementation. return realm.intrinsics.undefined; }); obj.defineNativeMethod("internalModuleStat", 0, (context, args) => { const fileName = _singletons.To.ToString(realm, args[0]); return new _index.NumberValue(realm, nativeFS.internalModuleStat(fileName)); }); obj.defineNativeMethod("lstat", 0, (context, args) => { const path = _singletons.To.ToString(realm, args[0]); (0, _invariant2.default)(args[1] instanceof _index.ObjectValue); const buffer = (0, _utils.getNodeBufferFromTypedArray)(realm, args[1]); const float64buffer = new Float64Array(buffer.buffer); nativeFS.lstat(path, float64buffer); return args[1]; }); obj.defineNativeMethod("fstat", 0, (context, args) => { const fd = _singletons.To.ToNumber(realm, args[0]); (0, _invariant2.default)(args[1] instanceof _index.ObjectValue); const buffer = (0, _utils.getNodeBufferFromTypedArray)(realm, args[1]); const float64buffer = new Float64Array(buffer.buffer); nativeFS.fstat(fd, float64buffer); return args[1]; }); obj.defineNativeMethod("open", 0, (context, args) => { const path = _singletons.To.ToString(realm, args[0]); const flags = _singletons.To.ToNumber(realm, args[1]); const mode = _singletons.To.ToNumber(realm, args[2]); const fd = nativeFS.open(path, flags, mode); return new _index.NumberValue(realm, fd); }); obj.defineNativeMethod("close", 0, (context, args) => { const fd = _singletons.To.ToNumber(realm, args[0]); nativeFS.close(fd); return realm.intrinsics.undefined; }); obj.defineNativeMethod("read", 0, (context, args) => { const fd = _singletons.To.ToNumber(realm, args[0]); (0, _invariant2.default)(args[1] instanceof _index.ObjectValue); const buffer = (0, _utils.getNodeBufferFromTypedArray)(realm, args[1]); const offset = _singletons.To.ToNumber(realm, args[2]); const length = _singletons.To.ToNumber(realm, args[3]); const position = args[4] === realm.intrinsics.undefined ? undefined : _singletons.To.ToNumber(realm, args[4]); const bytesRead = nativeFS.read(fd, buffer, offset, length, position); return new _index.NumberValue(realm, bytesRead); }); obj.defineNativeMethod("internalModuleReadFile", 0, (context, args) => { const path = _singletons.To.ToString(realm, args[0]); const result = nativeFS.internalModuleReadFile(path); if (result === undefined) { return realm.intrinsics.undefined; } return new _index.StringValue(realm, result); }); let FSReqWrapTemplateSrc = `${intrinsicName}.FSReqWrap`; let FSReqWrapTemplate = (0, _builder2.default)(FSReqWrapTemplateSrc); let val = _index.AbstractValue.createFromTemplate(realm, FSReqWrapTemplate, _index.ObjectValue, [], FSReqWrapTemplateSrc); val.values = new _index2.ValuesDomain(new Set([new _index.ObjectValue(realm)])); val.intrinsicName = FSReqWrapTemplateSrc; _singletons.Properties.DefinePropertyOrThrow(realm, obj, "FSReqWrap", { value: val, writable: true, configurable: true, enumerable: true }); // TODO: Implement more of the native methods here. Ideally all of them should // just be automatically proxied. return obj; }; var _invariant = require("../../invariant.js"); var _invariant2 = _interopRequireDefault(_invariant); var _index = require("../../values/index.js"); var _index2 = require("../../domains/index.js"); var _builder = require("../../utils/builder.js"); var _builder2 = _interopRequireDefault(_builder); var _utils = require("./utils.js"); var _singletons = require("../../singletons.js"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } //# sourceMappingURL=fs.js.map