Skip to content

Commit 57aaa6d

Browse files
author
Serhii Khoma
committed
feat(#3400): generate only es6 -> implement
1 parent a4f8aad commit 57aaa6d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1411
-878
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,5 @@ idris2docs_venv
5454

5555
# macOS
5656
.DS_Store
57+
node_modules
58+
package-lock.json

docs/source/backends/javascript.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
***********************************
2-
Javascript and Node Code Generators
2+
Javascript (Browser and Node) Code Generators
33
***********************************
44

55
There are two javascript code generators, ``node`` and ``javascript``. There are two
@@ -30,11 +30,19 @@ expression.
3030

3131

3232
.. code-block:: idris
33+
module My.Module
3334
34-
%foreign "node:lambda:fp=>require('fs').fstatSync(fp.fd, {bigint: false}).size"
35+
%foreign "node:support"
3536
prim__fileSize : FilePtr -> PrimIO Int
3637
37-
``require`` can be used to import javascript modules.
38+
And in `./support/node/My.Module.js`
39+
40+
.. code-block:: js
41+
import fs from 'node:fs'
42+
43+
export const prim__fileSize = fp => fs.fstatSync(fp.fd, {bigint: false}).size
44+
45+
``require`` cannot be used to import javascript modules.
3846

3947
For completion below an example of a foreign available only with ``browser`` codegen:
4048

docs/source/ffi/ffi.rst

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,30 @@ or ``javascript``. The former two are mutually exclusive while ``javascript``
8787
FFI specifiers apply both when building for the browser and when building for
8888
NodeJS.
8989

90-
Javascript specifiers must be further specialized as ``lambda``, ``support``,
91-
or ``stringIterator``.
90+
Javascript specifiers must be further specialized as ``lambda``, ``support``.
9291

9392
The syntax, therefore, is ``node:lambda:some_func`` (for the NodeJS-specific
9493
FFI and a lambda that executes a function named ``some_func``).
9594

9695
When using the ``support`` option, you also specify the name of the support
97-
file. Idris will look in all ``data`` directories under a ``js`` subfolder
96+
file. Idris will look in all ``data`` directories under a ``javascript`` subfolder
9897
for a file with this name. These file names should be distinct for your
9998
project so they don't collide with support files from other projects
10099
further on in the build process for an executable. Suppose your package is
101100
named "http-idris" and you have FFI specifiers like
102-
``node:support:http_request,http_idris`` in your Idris code. You should make
103-
sure a data directory in scope has a ``js`` directory with an
104-
``http_idris.js`` file in it. Another important note is that functions
105-
within this file must be prefixed with ``http_idris_``; therefore, the
106-
function referred to in the example we give here would need to be named
107-
``http_idris_http_request`` in the ``http_idris.js`` support file.
101+
``node:support`` in your Idris code. You should make
102+
sure a `support` directory has a ``node`` directory with an
103+
``My.Module.js`` file in it. Functions
104+
in ``My.Module.js`` file must have the same name as a name of Idris function.
105+
106+
For example if You will write in ./src/My/Module.idr:
107+
1. `%foreign "javascript:support" add : Int -> Int -> Int` will look in `./support/js/My.Module.js` for `export const add = (x, y) => x + y`
108+
2. `%foreign "node:support" add : Int -> Int -> Int` will look in `./support/node/My.Module.js` for `export const add = (x, y) => x + y`
109+
3. `%foreign "browser:support" add : Int -> Int -> Int` will look in `./support/browser/My.Module.js` for `export const add = (x, y) => x + y`
110+
4. `%foreign "browser:support:my_globally_unique_file_name" add : Int -> Int -> Int` will look in `./support/browser/my_globally_unique_file_name.js` for `export const add = (x, y) => x + y`
111+
5. `%foreign "browser:support:my_func,my_globally_unique_file_name" add : Int -> Int -> Int` will look in `./support/browser/my_globally_unique_file_name.js` for `export const my_func = (x, y) => x + y`
112+
113+
NOTE: If You use `%foreign "browser:support:my_globally_unique_file_name"`, then `my_globally_unique_file_name.js` should be unique in the whole internet. Suppose You installed `lib1` and `lib2` from github, suppose they both have `./support/browser/my_globally_unique_file_name.js`. Idris will try to copy `my_globally_unique_file_name.js` into `./build/exec/browser/my_globally_unique_file_name.js`. Which one it will prefer? From `lib1` or `lib2`? The lib that is first in `IDRIS2_DATA` path.
108114

109115
FFI Example
110116
-----------

eslint.config.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import globals from "globals";
2+
import pluginJs from "@eslint/js";
3+
import { includeIgnoreFile } from "@eslint/compat";
4+
import path from "node:path";
5+
import { fileURLToPath } from "node:url";
6+
7+
const __filename = fileURLToPath(import.meta.url);
8+
const __dirname = path.dirname(__filename);
9+
const gitignorePath = path.resolve(__dirname, ".gitignore");
10+
11+
/** @type {import('eslint').Linter.Config[]} */
12+
export default [
13+
includeIgnoreFile(gitignorePath),
14+
{ files: ["**/*.{js,mjs,cjs,ts}"] },
15+
{ languageOptions: { globals: { ...globals.browser, ...globals.node } } },
16+
pluginJs.configs.recommended,
17+
{
18+
rules: {
19+
"no-unused-vars": [
20+
"error",
21+
{
22+
args: "all",
23+
argsIgnorePattern: "^_",
24+
caughtErrors: "all",
25+
caughtErrorsIgnorePattern: "^_",
26+
destructuredArrayIgnorePattern: "^_",
27+
varsIgnorePattern: "^_",
28+
ignoreRestSiblings: true,
29+
},
30+
],
31+
},
32+
},
33+
];

flake.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@
8585
});
8686
inherit buildIdris;
8787
devShells.default = pkgs.mkShell.override { stdenv = stdenv'; } {
88-
packages = idris2Pkg.buildInputs;
88+
packages = [ idris2Pkg.buildInputs chez pkgs.python3 ];
89+
SCHEME="scheme";
8990
};
9091
};
9192
in lib.mkOvrOptsFlake

idris2api.ipkg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ modules =
2929
Compiler.ES.Ast,
3030
Compiler.ES.Codegen,
3131
Compiler.ES.Doc,
32-
Compiler.ES.Javascript,
32+
Compiler.ES.Browser,
3333
Compiler.ES.Node,
3434
Compiler.ES.State,
3535
Compiler.ES.TailRec,

libs/base/System.idr

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ import System.File
1414
supportC : (fn : String) -> String
1515
supportC fn = "C:\{fn}, libidris2_support, idris_support.h"
1616

17-
||| Shorthand for referring to the Node system support library
18-
|||
19-
||| @ fn the function name to refer to in the js/system_support.js file
20-
supportNode : (fn : String) -> String
21-
supportNode fn = "node:support:\{fn},support_system"
22-
2317
||| Shorthand for referring to libc 6
2418
|||
2519
||| @ fn the function name to refer to in libc 6
@@ -86,16 +80,16 @@ getArgs = do
8680
else pure []
8781

8882
%foreign libc "getenv"
89-
"node:lambda: n => process.env[n]"
83+
"node:lambda:n => process.env[n]"
9084
prim__getEnv : String -> PrimIO (Ptr String)
9185

9286
%foreign supportC "idris2_getEnvPair"
9387
prim__getEnvPair : Int -> PrimIO (Ptr String)
9488
%foreign supportC "idris2_setenv"
95-
supportNode "setEnv"
89+
"node:support"
9690
prim__setEnv : String -> String -> Int -> PrimIO Int
9791
%foreign supportC "idris2_unsetenv"
98-
supportNode "unsetEnv"
92+
"node:support"
9993
prim__unsetEnv : String -> PrimIO Int
10094

10195
%foreign "C:idris2_enableRawMode, libidris2_support, idris_support.h"
@@ -198,7 +192,7 @@ unsetEnv var
198192
pure $ ok == 0
199193

200194
%foreign "C:idris2_system, libidris2_support, idris_system.h"
201-
supportNode "spawnSync"
195+
"node:support"
202196
prim__system : String -> PrimIO Int
203197

204198
||| Execute a shell command, returning its termination status or -1 if an error

libs/base/System/Clock.idr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ clockTimeUtc = fromPrim prim__clockTimeUtc
136136

137137
%foreign "scheme:blodwen-clock-time-process"
138138
"RefC:clockTimeProcess"
139-
"javascript:support:clockTimeProcess,support_system_clock"
139+
"javascript:support"
140140
prim__clockTimeProcess : PrimIO OSClock
141141

142142
||| Get the amount of time used by the current process.
@@ -145,7 +145,7 @@ clockTimeProcess = fromPrim prim__clockTimeProcess
145145

146146
%foreign "scheme:blodwen-clock-time-thread"
147147
"RefC:clockTimeThread"
148-
"javascript:support:clockTimeThread,support_system_clock"
148+
"javascript:support"
149149
prim__clockTimeThread : PrimIO OSClock
150150

151151
||| Get the amount of time used by the current thread.

libs/base/System/Directory.idr

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ DirPtr = AnyPtr
1717
supportC : (fn : String) -> String
1818
supportC fn = "C:\{fn}, libidris2_support, idris_directory.h"
1919

20-
||| Shorthand for referring to the Node system support library
21-
|||
22-
||| @ fn the function name to refer to in the js/system_support.js file
23-
supportNode : (fn : String) -> String
24-
supportNode fn = "node:support:\{fn},support_system_directory"
25-
2620
ok : HasIO io => a -> io (Either FileError a)
2721
ok x = pure (Right x)
2822

@@ -31,27 +25,27 @@ ok x = pure (Right x)
3125
prim__currentDir : PrimIO (Ptr String)
3226

3327
%foreign supportC "idris2_changeDir"
34-
supportNode "changeDir"
28+
"node:support"
3529
prim__changeDir : String -> PrimIO Int
3630

3731
%foreign supportC "idris2_createDir"
38-
supportNode "createDir"
32+
"node:support"
3933
prim__createDir : String -> PrimIO Int
4034

4135
%foreign supportC "idris2_openDir"
42-
supportNode "openDir"
36+
"node:support"
4337
prim__openDir : String -> PrimIO DirPtr
4438

4539
%foreign supportC "idris2_closeDir"
46-
supportNode "closeDir"
40+
"node:support"
4741
prim__closeDir : DirPtr -> PrimIO ()
4842

4943
%foreign supportC "idris2_removeDir"
50-
supportNode "removeDir"
44+
"node:support"
5145
prim__removeDir : String -> PrimIO ()
5246

5347
%foreign supportC "idris2_nextDirEntry"
54-
supportNode "dirEntry"
48+
"node:support"
5549
prim__dirEntry : DirPtr -> PrimIO (Ptr String)
5650

5751
||| Data structure for managing the pointer to a directory.

libs/base/System/Errno.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module System.Errno
44
%default total
55

66
%foreign "C:idris2_getErrno, libidris2_support, idris_support.h"
7-
"node:support:getErrno,support_system"
7+
"node:support"
88
prim__getErrno : PrimIO Int
99

1010
%foreign "C:idris2_strerror, libidris2_support, idris_support.h"

libs/base/System/File/Buffer.idr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import Data.Buffer
1313
%default total
1414

1515
%foreign supportC "idris2_readBufferData"
16-
"node:lambda:(f,b,l,m) => require('fs').readSync(f.fd,b,l,m)"
16+
"node:support"
1717
prim__readBufferData : FilePtr -> Buffer -> (offset : Int) -> (maxbytes : Int) -> PrimIO Int
1818

1919
%foreign supportC "idris2_writeBufferData"
20-
"node:lambda:(f,b,l,m) => require('fs').writeSync(f.fd,b,l,m)"
20+
"node:support"
2121
prim__writeBufferData : FilePtr -> Buffer -> (offset : Int) -> (size : Int) -> PrimIO Int
2222

2323
||| Read the data from the file into the given buffer.

libs/base/System/File/Error.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import public System.File.Types
1212
prim__error : FilePtr -> PrimIO Int
1313

1414
%foreign supportC "idris2_fileErrno"
15-
supportNode "fileErrno"
15+
"node:support"
1616
prim__fileErrno : PrimIO Int
1717

1818
||| The types of errors that can occur during file operations.

libs/base/System/File/Handle.idr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import public System.File.Types
88
%default total
99

1010
%foreign supportC "idris2_openFile"
11-
supportNode "openFile"
11+
"node:support:system_open" -- why specify file? bc this function uses some dependency
1212
prim__open : String -> String -> PrimIO FilePtr
1313

1414
%foreign supportC "idris2_closeFile"
15-
"node:lambda:(fp) => require('fs').closeSync(fp.fd)"
15+
"node:support"
1616
prim__close : FilePtr -> PrimIO ()
1717

1818
||| Open the given file name with the specified mode.

libs/base/System/File/Meta.idr

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ FileTimePtr : Type
1616
FileTimePtr = AnyPtr
1717

1818
%foreign supportC "idris2_fileSize"
19-
"node:lambda:fp=>require('fs').fstatSync(fp.fd).size"
19+
"node:support"
2020
prim__fileSize : FilePtr -> PrimIO Int
2121

2222
%foreign supportC "idris2_fileSize"
2323
prim__fPoll : FilePtr -> PrimIO Int
2424

2525
%foreign supportC "idris2_fileTime"
26-
"node:support:filetime,support_system_file"
26+
"node:support"
2727
prim__fileTime : FilePtr -> PrimIO FileTimePtr
2828

2929
%foreign supportC "idris2_filetimeAccessTimeSec"
@@ -51,7 +51,7 @@ prim__filetimeStatusTimeSec : FileTimePtr -> PrimIO Int
5151
prim__filetimeStatusTimeNsec : FileTimePtr -> PrimIO Int
5252

5353
%foreign supportC "idris2_fileIsTTY"
54-
"node:lambda:fp=>Number(require('tty').isatty(fp.fd))"
54+
"node:support"
5555
prim__fileIsTTY : FilePtr -> PrimIO Int
5656

5757
||| Check if a file exists for reading.
@@ -154,4 +154,3 @@ fPoll (FHandle f)
154154
export
155155
isTTY : HasIO io => (h : File) -> io Bool
156156
isTTY (FHandle f) = (/= 0) <$> primIO (prim__fileIsTTY f)
157-

libs/base/System/File/Permissions.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import public System.File.Types
77
%default total
88

99
%foreign supportC "idris2_chmod"
10-
supportNode "chmod"
10+
"node:support"
1111
prim__chmod : String -> Int -> PrimIO Int
1212

1313
||| The UNIX file modes.

libs/base/System/File/Process.idr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import public System.File.Types
1111
"node:lambda:()=>0"
1212
prim__flush : FilePtr -> PrimIO Int
1313
%foreign supportC "idris2_popen"
14-
supportNode "popen"
14+
"node:support:system_open" -- why specify file? bc this function uses some dependency
1515
prim__popen : String -> String -> PrimIO FilePtr
1616
%foreign supportC "idris2_pclose"
17-
supportNode "pclose"
17+
"node:support:system_removeFileAndPclose" -- why specify file? bc this function uses some dependency
1818
prim__pclose : FilePtr -> PrimIO Int
1919

2020
data Popen2Result : Type where [external]

libs/base/System/File/ReadWrite.idr

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,29 @@ import System.FFI
1313
%default total
1414

1515
%foreign supportC "idris2_seekLine"
16-
supportNode "seekLine"
16+
"node:support"
1717
prim__seekLine : FilePtr -> PrimIO Int
1818

1919
%foreign supportC "idris2_readLine"
20-
supportNode "readLine"
20+
"node:support:system_readLine" -- why specify file? bc this function uses some dependency
2121
prim__readLine : FilePtr -> PrimIO (Ptr String)
2222

2323
%foreign supportC "idris2_readChars"
2424
prim__readChars : Int -> FilePtr -> PrimIO (Ptr String)
25+
2526
%foreign "C:fgetc,libc 6"
2627
prim__readChar : FilePtr -> PrimIO Int
2728

2829
%foreign supportC "idris2_writeLine"
29-
"node:lambda:(filePtr, line) => require('fs').writeSync(filePtr.fd, line, undefined, 'utf-8')"
30+
"node:support"
3031
prim__writeLine : FilePtr -> String -> PrimIO Int
3132

3233
%foreign supportC "idris2_eof"
3334
"node:lambda:f=>(f.eof?1:0)"
3435
prim__eof : FilePtr -> PrimIO Int
3536

3637
%foreign supportC "idris2_removeFile"
37-
supportNode "removeFile"
38+
"node:support:system_removeFileAndPclose" -- why specify file? bc this function uses some dependency
3839
prim__removeFile : String -> PrimIO Int
3940

4041
||| Seek through the next newline.

libs/base/System/File/Support.idr

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ public export
99
supportC : (fn : String) -> String
1010
supportC fn = "C:\{fn}, libidris2_support, idris_file.h"
1111

12-
||| Shorthand for referring to the Node system support library
13-
|||
14-
||| @ fn the function name to refer to in the js/system_support_file.js file
15-
public export
16-
supportNode : (fn : String) -> String
17-
supportNode fn = "node:support:\{fn},support_system_file"
18-
1912
||| Wrap x in the `Right` part of an `io . Either`.
2013
export
2114
ok : HasIO io => (x : a) -> io (Either err a)

libs/base/System/Info.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ isWindows : Bool
2323
isWindows = os `elem` ["windows", "mingw32", "cygwin32"]
2424

2525
%foreign "C:idris2_getNProcessors, libidris2_support, idris_support.h"
26-
"node:lambda:() => require('os').cpus().length"
26+
"node:support"
2727
prim__getNProcessors : PrimIO Int
2828

2929
||| Get the number of processors on the system. Returns `Nothing` if we somehow

0 commit comments

Comments
 (0)