CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tns-platform-declarations

Platform-specific TypeScript declarations for NativeScript for accessing native objects

84

1.23x
Overview
Eval results
Files

task.mdevals/scenario-5/

Binary Data Buffer Manager

Build a utility that performs low-level memory operations on binary data buffers using pointer arithmetic. The utility should allocate native memory buffers, perform byte-level operations using pointers, and manage memory safely.

Requirements

Create a module that provides the following functionality:

  1. Buffer Allocation: Allocate a native memory buffer of a specified size in bytes and return an object containing the pointer and size information.

  2. Byte Writing: Write a sequence of byte values to a buffer starting at a given offset. The function should use pointer arithmetic to navigate to the correct memory location.

  3. Byte Reading: Read a sequence of bytes from a buffer starting at a given offset and return them as a JavaScript array. Use pointer arithmetic to access the correct memory locations.

  4. Buffer Copy: Copy data from one buffer to another using pointer arithmetic. The function should take source and destination buffers with their offsets and copy a specified number of bytes.

Implementation Notes

  • All offset parameters are in bytes
  • Byte values should be in the range 0-255
  • Operations should validate that accesses don't exceed buffer boundaries

Test Cases

  • Allocating a 256-byte buffer returns a valid pointer and correct size @test
  • Writing bytes [0xFF, 0xAA, 0x55] at offset 0 and reading them back returns the same values @test
  • Writing bytes at offset 10 and reading from offset 10 returns correct values @test
  • Copying 8 bytes from source buffer offset 5 to destination buffer offset 3 transfers correct data @test

API

/**
 * Information about an allocated buffer
 */
export interface BufferInfo {
  pointer: any; // interop.AdoptedPointer
  size: number;
}

/**
 * Allocate a native memory buffer
 * @param size - Size in bytes to allocate
 * @returns Buffer information with pointer and size
 */
export function allocateBuffer(size: number): BufferInfo;

/**
 * Write bytes to a buffer at the specified offset
 * @param buffer - Buffer information
 * @param offset - Byte offset to start writing
 * @param bytes - Array of byte values (0-255) to write
 */
export function writeBytes(buffer: BufferInfo, offset: number, bytes: number[]): void;

/**
 * Read bytes from a buffer at the specified offset
 * @param buffer - Buffer information
 * @param offset - Byte offset to start reading
 * @param count - Number of bytes to read
 * @returns Array of byte values read
 */
export function readBytes(buffer: BufferInfo, offset: number, count: number): number[];

/**
 * Copy bytes from source buffer to destination buffer
 * @param srcBuffer - Source buffer information
 * @param srcOffset - Offset in source buffer
 * @param dstBuffer - Destination buffer information
 * @param dstOffset - Offset in destination buffer
 * @param count - Number of bytes to copy
 */
export function copyBytes(
  srcBuffer: BufferInfo,
  srcOffset: number,
  dstBuffer: BufferInfo,
  dstOffset: number,
  count: number
): void;

@generates

Dependencies { .dependencies }

tns-platform-declarations { .dependency }

Provides native interop capabilities for memory allocation and pointer operations.

Install with Tessl CLI

npx tessl i tessl/npm-tns-platform-declarations

tile.json