带有 Str 的 Perl6 NativeCall 被编码('utf16')得到随机损坏的结果
Perl6 NativeCall with Str is encoded('utf16') got randomly corrupted result
我正在使用 NativeCall
在 perl6 脚本中映射 GetFullPathName
windows API,因此我写了以下内容:
#!perl6
use NativeCall;
constant \WIN32_MAX_PATH = 260;
#I may use directly $path.IO.absolute()
sub Win32-GetFullPathName(
Str $lpFileName is encoded('utf16'),
int32 $nBufferLength,
#Str $lpBuffer is encoded('utf16') is rw,
Blob $lpBuffer is rw,
Str $lpFilenameIndex is rw)
returns int32
is native('kernel32.dll')
is symbol('GetFullPathNameW')
{ * }
my $path = '.';
my $fp-size = Win32-GetFullPathName(
$path, # $path ~ "[=11=]", # <!-- this hack make it working fine
WIN32_MAX_PATH,
#my Str $fpath = ' ' x WIN32_MAX_PATH;
my $buffer = buf16.allocate(WIN32_MAX_PATH),
Str );
put "[$fp-size] => ", $buffer.decode('utf16').substr(0, $fp-size);
代码随机运行,直到我在 $path
后附加一个 "[=14=]"
。
[编辑] 多次调用的结果:
[12] => D:\dev\pa.t
[12] => D:\dev\pa.
[12] => D:\dev\pa.槟
[9] => D:\dev\pa
[9] => D:\dev\pa
还有其他正确的方法吗?
我怀疑 src/strings/utf16.c
中存在 MoarVM 错误,特别是 line 403:
result = MVM_realloc(result, *output_size);
应该是
result = MVM_realloc(result, *output_size + 2);
如果您可以验证这是否解决了您的问题,请随时 file a bug report or even create a pull request。
我正在使用 NativeCall
在 perl6 脚本中映射 GetFullPathName
windows API,因此我写了以下内容:
#!perl6
use NativeCall;
constant \WIN32_MAX_PATH = 260;
#I may use directly $path.IO.absolute()
sub Win32-GetFullPathName(
Str $lpFileName is encoded('utf16'),
int32 $nBufferLength,
#Str $lpBuffer is encoded('utf16') is rw,
Blob $lpBuffer is rw,
Str $lpFilenameIndex is rw)
returns int32
is native('kernel32.dll')
is symbol('GetFullPathNameW')
{ * }
my $path = '.';
my $fp-size = Win32-GetFullPathName(
$path, # $path ~ "[=11=]", # <!-- this hack make it working fine
WIN32_MAX_PATH,
#my Str $fpath = ' ' x WIN32_MAX_PATH;
my $buffer = buf16.allocate(WIN32_MAX_PATH),
Str );
put "[$fp-size] => ", $buffer.decode('utf16').substr(0, $fp-size);
代码随机运行,直到我在 $path
后附加一个 "[=14=]"
。
[编辑] 多次调用的结果:
[12] => D:\dev\pa.t
[12] => D:\dev\pa.
[12] => D:\dev\pa.槟
[9] => D:\dev\pa
[9] => D:\dev\pa
还有其他正确的方法吗?
我怀疑 src/strings/utf16.c
中存在 MoarVM 错误,特别是 line 403:
result = MVM_realloc(result, *output_size);
应该是
result = MVM_realloc(result, *output_size + 2);
如果您可以验证这是否解决了您的问题,请随时 file a bug report or even create a pull request。