functionalscript 0.0.318 → 0.0.322

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.package)(moduleId.path.join('/'))(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,77 @@
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
+ const [r3, m3] = require_('x/r')(m2);
18
+ if (r3[0] === 'error') { throw r3 }
19
+ return [['ok', ':index.js'], m3]
20
+ }],
21
+ ':b.js': [
22
+ 'ok',
23
+ () => m0 => [['ok', ':b.js'], m0]],
24
+ ':a/index.js': [
25
+ 'ok',
26
+ () => m0 => [['ok', ':a/index.js'], m0]],
27
+ 'x:r.js': [
28
+ 'ok',
29
+ () => m0 => [['ok', 'x:r.js'], m0]],
30
+ }
31
+
32
+ /** @type {function_.Compile} */
33
+ const compile = source => compileMap[source] ?? ['error', 'invalid source']
34
+
35
+ /** @type {{ readonly [k in string]?: { readonly dependencies: { readonly [k in string]?: string }, files:{ readonly [k in string]?: string } }}} */
36
+ const packageMap = {
37
+ '': {
38
+ dependencies: {
39
+ 'x': '/node_modules/x'
40
+ },
41
+ files: {
42
+ 'index.js': ':index.js',
43
+ 'b.js': ':b.js',
44
+ 'a/index.js': ':a/index.js',
45
+ }
46
+ },
47
+ '/node_modules/x': {
48
+ dependencies: {},
49
+ files: {
50
+ 'r.js': 'x:r.js'
51
+ }
52
+ },
53
+ }
54
+
55
+ /** @type {package_.Get} */
56
+ const packageGet = packageId => {
57
+ const p = packageMap[packageId]
58
+ if (p === undefined) { return undefined }
59
+ return {
60
+ dependency: dependency => p.dependencies[dependency],
61
+ file: file => p.files[file]
62
+ }
63
+ }
64
+
65
+ const getOrBuild = _.getOrBuild
66
+ (compile)
67
+ (packageGet)
68
+ (/** @type {module_.MapInterface<map.Map<module_.State>>} */(map))
69
+
70
+ {
71
+ const [r] = getOrBuild({ package: '', path: ['index.js'] })(undefined)
72
+ if (JSON.stringify(r) !==
73
+ '["ok",{"exports":":index.js","requireMap":{"./a/":"/a/index.js","./b":"/b.js","x/r":"/node_modules/x/r.js"}}]'
74
+ ) {
75
+ throw r
76
+ }
77
+ }
@@ -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
  }
@@ -132,16 +132,15 @@ const parse = packageId => dependencies => local => path => {
132
132
 
133
133
  /**
134
134
  * @type {(packageGet: package_.Get) =>
135
- * (packageId: string) =>
136
- * (local: string) =>
135
+ * (moduleId: module_.Id) =>
137
136
  * (path: string) =>
138
137
  * Result
139
138
  * }
140
139
  */
141
- const parseAndFind = packageGet => packageId => local => path => {
142
- const currentPack = packageGet(packageId)
140
+ const parseAndFind = packageGet => moduleId => path => {
141
+ const currentPack = packageGet(moduleId.package)
143
142
  if (currentPack === undefined) { return undefined }
144
- const p = parse(packageId)(currentPack.dependency)(local)(path)
143
+ const p = parse(moduleId.package)(currentPack.dependency)(moduleId.path.join('/'))(path)
145
144
  if (p === undefined) { return undefined }
146
145
  const pack = packageGet(p.package)
147
146
  if (pack === undefined) { return undefined }
@@ -84,7 +84,7 @@ const stringify = g => {
84
84
  file: path => at(path)({ 'a/c': 'return "a/c"' }),
85
85
  }
86
86
  }
87
- const result = stringify(_.parseAndFind(p => at(p)(packages))('')('a/b')('../c'))
87
+ const result = stringify(_.parseAndFind(p => at(p)(packages))({ package: '', path: ['a', 'b']})('../c'))
88
88
  if (result !== '{"id":{"package":"","path":["a","c"]},"source":"return \\"a/c\\""}') { throw result }
89
89
  }
90
90
 
@@ -108,33 +108,33 @@ const stringify = g => {
108
108
  }
109
109
  }
110
110
  {
111
- const result = stringify(_.parseAndFind(p => at(p)(packages))('')('a/b')('z/a/c'))
111
+ const result = stringify(_.parseAndFind(p => at(p)(packages))({ package: '', path: ['a', 'b']})('z/a/c'))
112
112
  if (result !== '{"id":{"package":"node_modules/z","path":["a","c","index.js"]},"source":"return \\"a/c\\""}') {
113
113
  throw result
114
114
  }
115
115
  }
116
116
  {
117
- const result = stringify(_.parseAndFind(p => at(p)(packages))('')('a/b')('../..'))
117
+ const result = stringify(_.parseAndFind(p => at(p)(packages))({ package: '', path: ['a', 'b']})('../..'))
118
118
  if (result !== '{"id":{"package":"","path":["index.js"]},"source":"return \\"index.js\\""}') { throw result }
119
119
  }
120
120
  {
121
- const result = stringify(_.parseAndFind(p => at(p)(packages))('')('')('./x'))
121
+ const result = stringify(_.parseAndFind(p => at(p)(packages))({ package: '', path: []})('./x'))
122
122
  if (result !== '{"id":{"package":"","path":["x.js"]},"source":"return \\"x.js\\""}') { throw result }
123
123
  }
124
124
  {
125
- const result = stringify(_.parseAndFind(p => at(p)(packages))('')('')('./x.js'))
125
+ const result = stringify(_.parseAndFind(p => at(p)(packages))({ package: '', path: []})('./x.js'))
126
126
  if (result !== '{"id":{"package":"","path":["x.js"]},"source":"return \\"x.js\\""}') { throw result }
127
127
  }
128
128
  {
129
- const result = stringify(_.parseAndFind(p => at(p)(packages))('')('')('./x/'))
129
+ const result = stringify(_.parseAndFind(p => at(p)(packages))({ package: '', path: []})('./x/'))
130
130
  if (result !== '{"id":{"package":"","path":["x","index.js"]},"source":"return \\"x/index.js\\""}') { throw result }
131
131
  }
132
132
  {
133
- const result = stringify(_.parseAndFind(p => at(p)(packages))('')('x/a')('../'))
133
+ const result = stringify(_.parseAndFind(p => at(p)(packages))({ package: '', path: ['x', 'a']})('../'))
134
134
  if (result !== '{"id":{"package":"","path":["x","index.js"]},"source":"return \\"x/index.js\\""}') { throw result }
135
135
  }
136
136
  {
137
- const result = stringify(_.parseAndFind(p => at(p)(packages))('')('x/a')('..'))
137
+ const result = stringify(_.parseAndFind(p => at(p)(packages))({ package: '', path: ['x', 'a']})('..'))
138
138
  if (result !== '{"id":{"package":"","path":["x","index.js"]},"source":"return \\"x/index.js\\""}') { throw result }
139
139
  }
140
140
  }
@@ -158,11 +158,11 @@ const stringify = g => {
158
158
  }
159
159
  }
160
160
  {
161
- const result = stringify(_.parseAndFind(p => at(p)(packages))('')('a/b')('z/a/c'))
161
+ const result = stringify(_.parseAndFind(p => at(p)(packages))({ package: '', path: ['a', 'b']})('z/a/c'))
162
162
  if (result !== '{"id":{"package":"node_modules/z/a","path":["c.js"]},"source":"return \\"c.js\\""}') { throw result }
163
163
  }
164
164
  {
165
- const result = stringify(_.parseAndFind(p => at(p)(packages))('')('a/b')('z/a/c/'))
165
+ const result = stringify(_.parseAndFind(p => at(p)(packages))({ package: '', path: ['a', 'b']})('z/a/c/'))
166
166
  if (result !== '{"id":{"package":"node_modules/z/a","path":["c","index.js"]},"source":"return \\"c/index.js\\""}') { throw result }
167
167
  }
168
168
  }
@@ -186,6 +186,6 @@ const stringify = g => {
186
186
  }),
187
187
  }
188
188
  }
189
- const result = stringify(_.parseAndFind(p => at(p)(packages))('')('a/b')('z/a/c'))
189
+ const result = stringify(_.parseAndFind(p => at(p)(packages))({package: '', path: ['a', 'b']})('z/a/c'))
190
190
  if (result !== '{"id":{"package":"node_modules/z/a/c","path":["index.js"]},"source":"return \\"a/c\\""}') { throw result }
191
191
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.318",
3
+ "version": "0.0.322",
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' }