Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2920976
printf.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
909 B
Referenced Files
None
Subscribers
None
printf.ts
View Options
import
{
dlopen
,
suffix
,
FFIType
}
from
'ant:ffi'
;
let
libcName
:
string
;
if
(
process
.
platform
===
'darwin'
)
{
libcName
=
`libSystem.
${
suffix
}
`
;
}
else
if
(
process
.
platform
===
'linux'
)
{
libcName
=
`libc.
${
suffix
}
`
;
}
else
if
(
process
.
platform
===
'win32'
)
{
libcName
=
`msvcrt.
${
suffix
}
`
;
}
else
throw
new
Error
(
`Unsupported platform:
${
process
.
platform
}
`
);
interface
libC
{
putchar
(
c
:
number
)
:
number
;
printf
(
format
:
string
,
...args
:
unknown
[])
:
number
;
}
const
libc
=
dlopen
<
libC
>
(
libcName
);
libc
.
define
(
'putchar'
,
{
args
:
[
FFIType
.
int
],
returns
:
FFIType.int
});
libc
.
define
(
'printf'
,
{
args
:
[
FFIType
.
string
,
FFIType
.
spread
],
returns
:
FFIType.int
});
console
.
log
(
libc
);
console
.
log
(
'calling putchar(65):'
);
libc
.
putchar
(
65
);
// 'A'
console
.
log
(
'\ncalling printf:'
);
libc
.
printf
(
'Hello FFI! I see %d\n'
,
42
);
console
.
log
(
'calling putchar(66):'
);
libc
.
call
(
'putchar'
,
66
);
// 'B'
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Fri, Mar 27, 7:48 AM (2 d)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
512732
Default Alt Text
printf.ts (909 B)
Attached To
Mode
rANT Ant
Attached
Detach File
Event Timeline
Log In to Comment