functionalscript 0.0.319 → 0.0.320

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,11 +5,12 @@ const { todo } = require('../../dev')
5
5
  const map = require('../../types/map')
6
6
  const object = require('../../types/object')
7
7
  const path = require('../path')
8
+ const stringSet = require('../../types/stringSet')
8
9
 
9
10
  /**
10
11
  * @template M
11
12
  * @typedef {{
12
- * readonly pagkageGet: package_.Get
13
+ * readonly packageGet: package_.Get
13
14
  * readonly moduleMapInterface: module_.MapInterface<M>
14
15
  * readonly moduleId: module_.Id
15
16
  * readonly moduleMap: M
@@ -35,26 +36,43 @@ const notFound = moduleMap => [['error', ['file not found']], moduleMap]
35
36
  */
36
37
  const getOrBuild = compile => packageGet => moduleMapInterface => {
37
38
  /** @typedef {typeof moduleMapInterface extends module_.MapInterface<infer M> ? M : never} M */
38
- /** @type {(moduleId: module_.Id) => function_.Require<[map.Map<string>, M]>} */
39
- const req = moduleId => p => prior => {
40
- const r = path.parseAndFind(packageGet)(moduleId)(p)
41
- if (r === undefined) { return [['error', 'file not found'], prior] }
42
- const requireMap = map.set(p)(module_.idToString(moduleId))(prior[0])
43
- r
44
- return todo()
39
+
40
+ /** @type {(buildSet: stringSet.StringSet) => (moduleId: module_.Id) => (source: string) => (moduleMap: M) => Result<M>} */
41
+ const build = buildSet => moduleId => {
42
+ const moduleIdStr = module_.idToString(moduleId)
43
+ const buildSet1 = stringSet.set(moduleIdStr)(buildSet)
44
+ const dir = module_.dir(moduleId)
45
+ /** @type {function_.Require<readonly[map.Map<string>, M]>} */
46
+ const require_ = p => ([requireMap, m]) => {
47
+ /** @type {(e: unknown) => function_.Result<readonly[map.Map<string>, M]>} */
48
+ const error = e => [['error', 'file not found'], [requireMap, m]]
49
+ if (dir === undefined) { return error('file not found') }
50
+ const r = path.parseAndFind(packageGet)(dir)(p)
51
+ if (r === undefined) { return error('file not found') }
52
+ const rIdStr = module_.idToString(r.id)
53
+ if (stringSet.contains(rIdStr)(buildSet1)) { return error('circular reference') }
54
+ const [state, m1] = build(buildSet1)(r.id)(r.source)(m)
55
+ return [state[0] === 'error' ? state : ['ok', state[1].exports], [map.set(p)(rIdStr)(requireMap), m1]]
56
+ }
57
+ return source => moduleMap => {
58
+ /** @type {(s: module_.State) => (m: M) => Result<M>} */
59
+ const set = s => m => [s, moduleMapInterface.set(moduleIdStr)(s)(m)]
60
+ /** @type {(e: module_.Error) => (m: M) => Result<M>} */
61
+ const error = e => set(['error', e])
62
+ // check compilation
63
+ const j = compile(source)
64
+ if (j[0] === 'error') { return error(['compilation error', j[1]])(moduleMap) }
65
+ // build
66
+ const [r, [requireMap, moduleMap2]] = j[1](require_)([undefined, moduleMap])
67
+ const x = r[0] === 'error' ?
68
+ error(['runtime error', r[1]]) :
69
+ set(['ok', { exports: r[1], requireMap: object.fromMap(requireMap) }])
70
+ return x(moduleMap2)
71
+ }
45
72
  }
46
- /**
47
- * @type {(moduleId: module_.Id) =>
48
- * (moduleMapFirst: M) =>
49
- * Result<M>
50
- * }
51
- */
73
+ /** @type {(moduleId: module_.Id) => (moduleMap: M) => Result<M>} */
52
74
  const f = moduleId => moduleMap => {
53
75
  const moduleIdStr = module_.idToString(moduleId)
54
- /** @type {(s: module_.State) => (m: M) => Result<M>} */
55
- const set = s => m => [s, moduleMapInterface.insert(moduleIdStr)(s)(m)]
56
- /** @type {(e: module_.Error) => (m: M) => Result<M>} */
57
- const error = e => set(['error', e])
58
76
  // check moduleMap
59
77
  {
60
78
  const m = moduleMapInterface.at(moduleIdStr)(moduleMap)
@@ -64,114 +82,12 @@ const getOrBuild = compile => packageGet => moduleMapInterface => {
64
82
  const p = packageGet(moduleId.package)
65
83
  if (p === undefined) { return notFound(moduleMap) }
66
84
  // check file
67
- const f = p.file(moduleId.path.join('/'))
68
- if (f === undefined) { return notFound(moduleMap) }
69
- // check compilation
70
- const j = compile(f)
71
- if (j[0] === 'error') { return error(['compilation error', j[1]])(moduleMap) }
72
- // build
73
- const [r, [requireMap, moduleMap2]] = j[1](req(moduleId))([undefined, moduleMap])
74
- const x = r[0] === 'error' ?
75
- error(['runtime error', r[1]]) :
76
- set(['ok', { exports: r[1], requireMap: object.fromEntries(map.entries(requireMap)) }])
77
- return x(moduleMap2)
85
+ const source = p.file(moduleId.path.join('/'))
86
+ return (source === undefined ? notFound : build(undefined)(moduleId)(source))(moduleMap)
78
87
  }
79
88
  return f
80
89
  }
81
90
 
82
- // /**
83
- // * @type {(packageGet: package_.Get) =>
84
- // * <M>(moduleMapInterface: module_.MapInterface<M>) =>
85
- // * (compile: function_.Compile) =>
86
- // * (moduleId: module_.Id) =>
87
- // * (moduleMapFirst: M) =>
88
- // * Result<M>
89
- // * }
90
- // */
91
- // const getOrBuild = packageGet => moduleMapInterface => compile => moduleId => {
92
-
93
- // /** @typedef {typeof moduleMapInterface extends module_.MapInterface<infer M> ? M : never} M */
94
-
95
- // const moduleIdStr = module_.idToString(moduleId)
96
-
97
- // /** @type {(e: module_.Error) => (moduleMap: M) => Result<M>} */
98
- // const error = e => moduleMap => {
99
- // /** @type {module_.State} */
100
- // const state = ['error', e]
101
- // return [state, moduleMapInterface.insert(moduleIdStr)(state)(moduleMap)]
102
- // }
103
-
104
- // /** @type {function_.Require<readonly[M, map.Map<string>]>} */
105
- // const require_ = pathStr => prior => {
106
- // const pathResult = path.parseAndFind(packageGet)(moduleId.packageId)(moduleIdStr)(pathStr)
107
- // if (pathResult === undefined) { return [['error', `file not found: '${pathStr}'`], prior] }
108
- // const mId = { packageId: pathResult.package, path: pathResult.file.split('/') }
109
- // const [state, newMap] = getOrBuild
110
- // (packageGet)
111
- // (moduleMapInterface)
112
- // (compile)
113
- // (mId)
114
- // (prior[0])
115
- // switch (state[0]) {
116
- // case 'ok': {
117
- // const newRequireMap = map.set(pathStr)(module_.idToString(mId))(prior[1])
118
- // return [
119
- // ['ok', state[1].exports],
120
- // [newMap, newRequireMap]
121
- // ]
122
- // }
123
- // case 'building': {
124
- // return todo()
125
- // }
126
- // // 'ok'
127
- // default: {
128
- // return todo()
129
- // }
130
- // }
131
- // }
132
-
133
- // return moduleMapFirst => {
134
- // let moduleMap = moduleMapFirst
135
-
136
- // {
137
- // const m = moduleMapInterface.at(moduleIdStr)(moduleMap)
138
- // if (m !== undefined) { return [m, moduleMap] }
139
- // }
140
-
141
- // const p = packageGet(moduleId.packageId)
142
- // if (p === undefined) { return notFound(moduleMap) }
143
-
144
- // const source = p.file(moduleId.path.join('/'))
145
- // if (source === undefined) { return notFound(moduleMap) }
146
-
147
- // const compileResult = compile(source)
148
- // if (compileResult[0] === 'error') { return error(['compilation error', compileResult[1]])(moduleMap) }
149
-
150
- // const moduleFunction = compileResult[1]
151
-
152
- // moduleMap = moduleMapInterface.insert(moduleIdStr)(['building'])(moduleMap)
153
-
154
- // const [[type, exportsOrError], [newModuleMap, requireMap]] = moduleFunction(require_)([moduleMap, map.empty])
155
- // moduleMap = newModuleMap
156
-
157
- // {
158
- // const m = moduleMapInterface.at(moduleIdStr)(moduleMap)
159
- // if (m === undefined ) { throw 'm === undefined' }
160
- // if (m[0] !== 'building') { return [m, moduleMap] }
161
- // }
162
-
163
- // /** @type {module_.State} */
164
- // const result = type === 'error' ?
165
- // ['error', ['runtime error', exportsOrError]] :
166
- // ['ok', {
167
- // exports: exportsOrError,
168
- // requireMap: object.fromMap(requireMap)
169
- // }]
170
-
171
- // return [result, moduleMapInterface.insert(moduleIdStr)(result)(moduleMap)]
172
- // }
173
- // }
174
-
175
91
  module.exports = {
176
92
  /** @readonly */
177
93
  getOrBuild,
@@ -0,0 +1,65 @@
1
+ const _ = require('.')
2
+ const map = require('../../types/map')
3
+ const module_ = require('../module')
4
+ const function_ = require('../module/function')
5
+ const result = require('../../types/result')
6
+ const package_ = require('../package')
7
+
8
+ /** @type {{ readonly [k in string]?: result.Result<function_.Function_, unknown> }} */
9
+ const compileMap = {
10
+ ':index.js': [
11
+ 'ok',
12
+ require_ => m0 => {
13
+ const [r1, m1] = require_('./b')(m0);
14
+ if (r1[0] === 'error') { throw r1 }
15
+ const [r2, m2] = require_('./a/')(m1);
16
+ if (r2[0] === 'error') { throw r2 }
17
+ return [['ok', ':index.js'], m2]
18
+ }],
19
+ ':b.js': [
20
+ 'ok',
21
+ () => m0 => {
22
+ return [['ok', ':b.js'], m0]
23
+ }],
24
+ ':a/index.js': [
25
+ 'ok',
26
+ () => m0 => {
27
+ return [['ok', ':a/index.js'], m0]
28
+ }]
29
+ }
30
+
31
+ /** @type {function_.Compile} */
32
+ const compile = source => compileMap[source] ?? ['error', 'invalid source']
33
+
34
+ /** @type {{ readonly [k in string]?: { readonly [k in string]?: string } }} */
35
+ const packageMap = {
36
+ '': {
37
+ 'index.js': ':index.js',
38
+ 'b.js': ':b.js',
39
+ 'a/index.js': ':a/index.js',
40
+ }
41
+ }
42
+
43
+ /** @type {package_.Get} */
44
+ const packageGet = packageId => {
45
+ const p = packageMap[packageId]
46
+ if (p === undefined) { return undefined }
47
+ return {
48
+ dependency: () => undefined,
49
+ file: file => p[file]
50
+ }
51
+ }
52
+
53
+ const getOrBuild = _.getOrBuild
54
+ (compile)
55
+ (packageGet)
56
+ (/** @type {module_.MapInterface<map.Map<module_.State>>} */(map))
57
+
58
+ {
59
+ const [r] = getOrBuild({ package: '', path: ['index.js'] })(undefined)
60
+ if (JSON.stringify(r) !==
61
+ '["ok",{"exports":":index.js","requireMap":{"./a/":"/a/index.js","./b":"/b.js"}}]'
62
+ ) {
63
+ throw r
64
+ }
65
+ }
@@ -4,7 +4,7 @@ const object = require('../../types/object')
4
4
  * @template M
5
5
  * @typedef {{
6
6
  * readonly at: (moduleId: string) => (moduleMap: M) => State | undefined
7
- * readonly insert: (moduleId: string) => (moduleState: State) => (moduleMap: M) => M
7
+ * readonly set: (moduleId: string) => (moduleState: State) => (moduleMap: M) => M
8
8
  * }} MapInterface
9
9
  */
10
10
 
@@ -38,10 +38,22 @@ const object = require('../../types/object')
38
38
  * }} Id
39
39
  */
40
40
 
41
+ /** @type {(id: Id) => Id | undefined} */
42
+ const dir = id => {
43
+ const len = id.path.length
44
+ if (len < 1) { return undefined }
45
+ return {
46
+ package: id.package,
47
+ path: id.path.slice(0, len - 1)
48
+ }
49
+ }
50
+
41
51
  /** @type {(id: Id) => string} */
42
52
  const idToString = id => `${id.package}/${id.path.join('/')}`
43
53
 
44
54
  module.exports = {
55
+ /** @readonly */
56
+ dir,
45
57
  /** @readonly */
46
58
  idToString,
47
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.319",
3
+ "version": "0.0.320",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test.js CHANGED
@@ -12,6 +12,7 @@ require('./commonjs/package/test')
12
12
  require('./commonjs/path/test')
13
13
  require('./types/function/compare/test')
14
14
  require('./types/stringSet/test')
15
+ require('./commonjs/build/test')
15
16
 
16
17
  /** @type {() => never} */
17
18
  const assert = () => { throw 'assert' }