Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export function ReadProperty() {
- const object = {
- x: 5,
- };
- return function () {
- return object.x;
- };
- }
- export function ReadArrayBuffer() {
- const buffer = new ArrayBuffer(4096);
- const dataView = new DataView(buffer, 2048);
- dataView.setInt16(16, 5);
- return function () {
- return dataView.getInt16(16);
- };
- }
- export function DefineProperty() {
- const buffer = new ArrayBuffer(4096);
- const dataView = new DataView(buffer, 2048);
- dataView.setInt16(16, 5);
- const object = {
- dataView,
- };
- Object.defineProperty(object, 'x', {
- get: function () {
- return this.dataView.getInt16(16);
- },
- });
- return function () {
- return object.x;
- };
- }
- export function DefinePropertyClass() {
- const buffer = new ArrayBuffer(4096);
- const dataView = new DataView(buffer, 2048);
- dataView.setInt16(16, 5);
- class Chunk {
- constructor(dataView) {
- this.dataView = dataView;
- }
- }
- Chunk.prototype.decoder = {
- fields: [
- {field: "x", offset: 16, type: "Int16"},
- {field: "y", offset: 2, type: "Int16"},
- ],
- };
- const chunk = new Chunk(dataView);
- for (let {field, offset, type} of chunk.decoder.fields) {
- Object.defineProperty(chunk, field, {
- get: function () {
- return this.dataView[`get${type}`](offset);
- },
- });
- }
- return function () {
- return chunk.x;
- };
- }
- export function ArrayBufferWithProxy() {
- const buffer = new ArrayBuffer(4096);
- const dataView = new DataView(buffer, 2048);
- class Chunk {
- constructor(dataView) {
- this.dataView = dataView;
- }
- }
- Chunk.prototype.cache = {};
- Chunk.prototype.decoder = {
- "x": {offset: 0, type: "Int16"},
- "y": {offset: 2, type: "Int16"},
- };
- const chunk = new Proxy(new Chunk(dataView), {
- get: function (target, property, receiver) {
- if (target[property] !== undefined) {
- return target[property];
- }
- if (target['cache'][property] === undefined) {
- const readerInfo = target['decoder'][property];
- if (readerInfo === undefined) {
- return undefined;
- }
- const {offset, type} = readerInfo;
- const {dataView} = target;
- const getter = dataView[`get${type}`];
- const value = getter.call(dataView, offset);
- }
- return target['cache'][property];
- }
- });
- return function () {
- return chunk.x;
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement