From ea172754fb3774190086bdbc9bf0ad42b9a52676 Mon Sep 17 00:00:00 2001 From: waiting <1661926154@qq.com> Date: Tue, 28 Jun 2022 18:24:31 +0800 Subject: [PATCH] optimization(win32-api): BufferTypeFactory() --- packages/win32-api/src/lib/fixed-buffer.ts | 59 +++++++++++----------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/packages/win32-api/src/lib/fixed-buffer.ts b/packages/win32-api/src/lib/fixed-buffer.ts index 01afc8e7..190c11dc 100644 --- a/packages/win32-api/src/lib/fixed-buffer.ts +++ b/packages/win32-api/src/lib/fixed-buffer.ts @@ -19,6 +19,8 @@ export interface BufferType extends Type { * @ref https://gist.github.com/TooTallNate/80ac2d94b950216a2705 */ export function BufferTypeFactory(length: number, encoding?: BufferEncoding): BufferType { + assert(length >= 0) + const inst = Object.create(types.byte, { constructor: { configurable: true, @@ -26,36 +28,35 @@ export function BufferTypeFactory(length: number, encoding?: BufferEncoding): Bu writable: true, value: BufferTypeFactory, }, - }) as BufferType - Object.defineProperty(inst, 'size', { - configurable: true, - enumerable: true, - writable: false, - value: length, - }) - - Object.defineProperty(inst, 'encoding', { - configurable: true, - enumerable: true, - writable: true, - value: encoding, - }) - - - Object.defineProperty(inst, 'get', { - configurable: true, - enumerable: true, - writable: true, - value: getFn, - }) - - Object.defineProperty(inst, 'set', { - configurable: true, - enumerable: true, - writable: true, - value: setFn, - }) + size: { + configurable: true, + enumerable: true, + writable: false, + value: length, + }, + + encoding: { + configurable: true, + enumerable: true, + writable: true, + value: encoding, + }, + + get: { + configurable: true, + enumerable: true, + writable: true, + value: getFn, + }, + + set: { + configurable: true, + enumerable: true, + writable: true, + value: setFn, + }, + }) as BufferType return inst }