Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 ffi: ptrView.copyInto(arr) now causes a TypeError (regression between v1.37.1 and v1.37.2) #21005

Closed
rivy opened this issue Oct 28, 2023 · 0 comments · Fixed by #21006
Closed
Assignees

Comments

@rivy
Copy link
Contributor

rivy commented Oct 28, 2023

export type WString = Uint16Array;

const isWinOS = Deno.build.os === 'windows';

const dll = isWinOS ? dlopen(
		'kernel32.dll',
		/* fns */
		{
			'GetCommandLineA': { parameters: [], result: 'pointer' },
			'GetCommandLineW': { parameters: [], result: 'pointer' },
		},
	)
	: undefined;

function GetCommandLineW(): WString | undefined {
	// @ts-ignore # Deno.PointerValue is unstable, available in Deno v1.24.2+
	const ptr = dll?.symbols.GetCommandLineW() as Deno.PointerValue;
	const ptrView = ptr && new unstable.UnsafePointerView(ptr);
	if (ptrView == null) return undefined;

	const Uint16ByteSize = Uint16Array.BYTES_PER_ELEMENT;
	let byteOffset = 0;
	let value = ptrView.getUint16(byteOffset);
	while (value != null && value !== 0) {
		byteOffset = byteOffset + Uint16ByteSize;
		value = ptrView.getUint16(byteOffset);
	}
	const s = new Uint16Array(byteOffset / Uint16ByteSize);
	ptrView.copyInto(s); // Deno 1.37.2 => TypeError: expected typed ArrayBufferView
        // Deno v1.37.2+, use...
	// let idx = 0;
	// byteOffset = 0;
	// value = ptrView.getUint16(byteOffset);
	// while ((value != null && value !== 0)) {
	//	s[idx++] = value;
	//	byteOffset = byteOffset + Uint16ByteSize;
	//	value = ptrView.getUint16(byteOffset);
	//}
	return s;
}
  • Deno v1.37.1 => correct output
  • Deno v1.37.2 =>
error: Uncaught (in promise) TypeError: expected typed ArrayBufferView
        ptrView.copyInto(s); // Deno 1.37.2 => TypeError: expected typed ArrayBufferView
                ^
    at UnsafePointerView.copyInto (ext:deno_ffi/00_ffi.js:186:9)
    at GetCommandLineW (file:///C:/Users/Roy/AARK/Projects/deno/dxx/repo.GH/src/lib/commandLine.ts:79:10)
    at Module.GetCommandLine (file:///C:/Users/Roy/AARK/Projects/deno/dxx/repo.GH/src/lib/commandLine.ts:95:25)
    at file:///C:/Users/Roy/AARK/Projects/deno/dxx/repo.GH/src/lib/xProcess.ts:201:41
    at eventLoopTick (ext:core/01_core.js:183:11)

The noted alternative manual copy code works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants