Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2944375
mathlib.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
mathlib.js
View Options
import
{
join
}
from
'ant:path'
;
import
{
dlopen
,
suffix
,
FFIType
}
from
'ant:ffi'
;
// compile the C library first:
// clang -shared -fPIC -o mathlib.(so/dylib/dll) mathlib.c
const
mathlib
=
dlopen
(
join
(
import
.
meta
.
dirname
,
`mathlib.
${
suffix
}
`
));
mathlib
.
define
(
'add'
,
{
args
:
[
FFIType
.
int
,
FFIType
.
int
],
returns
:
FFIType
.
int
});
mathlib
.
define
(
'multiply'
,
{
args
:
[
FFIType
.
int
,
FFIType
.
int
],
returns
:
FFIType
.
int
});
mathlib
.
define
(
'greet'
,
{
args
:
[
FFIType
.
string
],
returns
:
FFIType
.
void
});
mathlib
.
define
(
'divide'
,
{
args
:
[
FFIType
.
double
,
FFIType
.
double
],
returns
:
FFIType
.
double
});
const
result1
=
mathlib
.
call
(
'add'
,
5
,
3
);
console
.
log
(
`add(5, 3) =
${
result1
}
`
);
const
result2
=
mathlib
.
call
(
'multiply'
,
4
,
7
);
console
.
log
(
`multiply(4, 7) =
${
result2
}
`
);
console
.
log
(
'Calling greet function:'
);
mathlib
.
call
(
'greet'
,
'World'
);
const
result3
=
mathlib
.
call
(
'divide'
,
10.0
,
2.0
);
console
.
log
(
`divide(10.0, 2.0) =
${
result3
}
`
);
const
result4
=
mathlib
.
call
(
'divide'
,
10.0
,
0.0
);
console
.
log
(
`divide(10.0, 0.0) =
${
result4
}
`
);
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Sat, Mar 28, 7:31 AM (1 d, 19 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
513137
Default Alt Text
mathlib.js (1 KB)
Attached To
Mode
rANT Ant
Attached
Detach File
Event Timeline
Log In to Comment