functionalscript 0.0.264 → 0.0.265

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.
@@ -52,4 +52,8 @@ type BuildState<M> = {
52
52
  }
53
53
 
54
54
  const getOrBuild: <M>(buildConfig: BuildConfig<M>) => readonly[ModuleState, M];
55
+
56
+ //
57
+
58
+
55
59
  ```
@@ -0,0 +1,62 @@
1
+ const package_ = require('../package')
2
+ const module_ = require('../module')
3
+ const function_ = require('../module/function')
4
+ const { todo } = require('../../dev')
5
+
6
+ /**
7
+ * @template M
8
+ * @typedef {{
9
+ * readonly pagkageGet: package_.Get
10
+ * readonly moduleMapInterface: module_.MapInterface<M>
11
+ * readonly moduleId: module_.Id
12
+ * readonly moduleMap: M
13
+ * }} Config
14
+ */
15
+
16
+ /**
17
+ * @template M
18
+ * @typedef {readonly[module_.State, M]} Result
19
+ */
20
+
21
+ /**
22
+ * @type {(packageGet: package_.Get) =>
23
+ * <M>(moduleMapInterface: module_.MapInterface<M>) =>
24
+ * (compile: function_.Compile) =>
25
+ * (moduleId: module_.Id) =>
26
+ * (moduleMap: M) =>
27
+ * Result<M>
28
+ * }
29
+ */
30
+ const getOrBuild = packageGet => moduleMapInterface => compile => moduleId => moduleMap => {
31
+ const moduleIdStr = module_.idToString(moduleId)
32
+
33
+ /** @type {() => Result<typeof moduleMap>} */
34
+ const notFound = () => [['error', ['file not found']], moduleMap]
35
+
36
+ /** @type {(e: module_.Error) => Result<typeof moduleMap>} */
37
+ const error = e => {
38
+ /** @type {module_.State} */
39
+ const state = ['error', e]
40
+ moduleMapInterface.insert(moduleIdStr)(state)
41
+ return [state, moduleMap]
42
+ }
43
+
44
+ const m = moduleMapInterface.at(moduleIdStr)(moduleMap)
45
+ if (m !== undefined) { return [m, moduleMap] }
46
+
47
+ const p = packageGet(moduleId.packageId)
48
+ if (p === undefined) { return notFound() }
49
+
50
+ const source = p.file(moduleId.path.join('/'))
51
+ if (source === undefined) { return notFound() }
52
+
53
+ const compileResult = compile(source)
54
+ if (compileResult[0] === 'error') { return error(['compilation error', compileResult[1]]) }
55
+
56
+ return todo()
57
+ }
58
+
59
+ module.exports = {
60
+ /** @readonly */
61
+ getOrBuild,
62
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * An IO interface for creating and running module functions.
3
+ */
4
+
5
+ const result = require('../../../types/result')
6
+
7
+ /** @typedef {<M>(require: Require<M>) => (prior: M) => Result<M>} Function */
8
+
9
+ /**
10
+ * @template M
11
+ * @typedef {readonly[result.Result<unknown, unknown>, M]} Result
12
+ */
13
+
14
+ /**
15
+ * @template M
16
+ * @typedef {(path: string) => (prior: M) => Result<M>} Require
17
+ */
18
+
19
+ /** @typedef {(source: string) => result.Result<Function, unknown>} Compile */
20
+
21
+ module.exports = {}
@@ -3,17 +3,17 @@ const object = require('../../types/object')
3
3
  /**
4
4
  * @template M
5
5
  * @typedef {{
6
- * readonly at: (moduleId: string) => (moduleMap: M) => ModuleState | undefined
7
- * readonly insert: (moduleId: string) => (moduleState: ModuleState) => (moduleMap: M) => M
8
- * }} ModuleMapInterface
6
+ * readonly at: (moduleId: string) => (moduleMap: M) => State | undefined
7
+ * readonly insert: (moduleId: string) => (moduleState: State) => (moduleMap: M) => M
8
+ * }} MapInterface
9
9
  */
10
10
 
11
11
  /**
12
12
  * @typedef {|
13
13
  * readonly['ok', Module] |
14
- * readonly['error', ModuleError] |
14
+ * readonly['error', Error] |
15
15
  * readonly['building']
16
- * } ModuleState
16
+ * } State
17
17
  */
18
18
 
19
19
  /**
@@ -25,11 +25,24 @@ const object = require('../../types/object')
25
25
 
26
26
  /**
27
27
  * @typedef {|
28
- * 'file not found' |
29
- * 'compile error' |
30
- * 'runtime error' |
31
- * 'circular reference'
32
- * } ModuleError
28
+ * ['file not found'] |
29
+ * ['compilation error', unknown] |
30
+ * ['runtime error'] |
31
+ * ['circular reference']
32
+ * } Error
33
33
  */
34
34
 
35
- module.exports = {}
35
+ /**
36
+ * @typedef {{
37
+ * readonly packageId: string
38
+ * readonly path: readonly string[]
39
+ * }} Id
40
+ */
41
+
42
+ /** @type {(id: Id) => string} */
43
+ const idToString = ({ packageId, path }) => `${packageId}/${path.join('/')}`
44
+
45
+ module.exports = {
46
+ /** @readonly */
47
+ idToString,
48
+ }
@@ -1,13 +1,11 @@
1
1
  const json = require('../../json')
2
- const dep = require('./dependencies')
3
- const object = require('../../types/object')
4
- const run = require('../run')
2
+ const dependencies = require('./dependencies')
5
3
 
6
4
  /**
7
5
  * @typedef {{
8
6
  * readonly name: string
9
7
  * readonly version: string
10
- * readonly dependencies?: dep.DependenciesJson
8
+ * readonly dependencies?: dependencies.DependenciesJson
11
9
  * }} PackageJson
12
10
  */
13
11
 
@@ -16,7 +14,7 @@ const isPackageJson = j => {
16
14
  if (!json.isObject(j)) { return false }
17
15
  if (typeof j.name !== 'string') { return false }
18
16
  if (typeof j.version !== 'string') { return false }
19
- if (!dep.isDependenciesJson(j.dependencies)) { return false }
17
+ if (!dependencies.isDependenciesJson(j.dependencies)) { return false }
20
18
  return true
21
19
  }
22
20
 
@@ -27,7 +25,7 @@ const isPackageJson = j => {
27
25
  * }} Package
28
26
  */
29
27
 
30
- /** @typedef {(packageId: string) => Package | undefined} PackageGet */
28
+ /** @typedef {(packageId: string) => Package | undefined} Get */
31
29
 
32
30
  module.exports = {
33
31
  /** @readonly */
@@ -1,12 +1,14 @@
1
1
  const { tryCatch } = require('../result')
2
2
  const { unwrap } = require('../../types/result')
3
- const run = require('../../commonjs/run')
3
+ const moduleFunction = require('../../commonjs/module/function')
4
4
 
5
- /** @type {(f: Function) => run.Module} */
5
+ /** @type {(f: Function) => moduleFunction.Function} */
6
6
  const build = f => immutableRequire => mutableData => {
7
7
  /** @type {(path: string) => unknown} */
8
8
  const mutableRequire = path => {
9
9
  const [result, data] = immutableRequire(path)(mutableData)
10
+ // Side effect: setting a variable from a nested function (closure)
11
+ // is not allowed in FunctionalScript.
10
12
  mutableData = data
11
13
  return unwrap(result)
12
14
  }
@@ -18,8 +20,9 @@ const build = f => immutableRequire => mutableData => {
18
20
  return [result, mutableData]
19
21
  }
20
22
 
21
- /** @type {run.Compile} */
23
+ /** @type {moduleFunction.Compile} */
22
24
  const compile = source =>
25
+ // Side effect: a `Function` constructor is not allowed in FunctionalScript.
23
26
  tryCatch(() => build(Function('module', 'require', `"use strict";${source}`)))
24
27
 
25
28
  module.exports = {
@@ -1,5 +1,5 @@
1
1
  const _ = require('.')
2
- const run = require('../../commonjs/run')
2
+ const run = require('../../commonjs/module/function')
3
3
 
4
4
  // ok:
5
5
  {
@@ -2,6 +2,7 @@ const result = require('../../types/result')
2
2
 
3
3
  /** @type {<T>(f: () => T) => result.Result<T, unknown>} */
4
4
  const tryCatch = f => {
5
+ // Side effect: `try catch` is not allowed in FunctionalScript.
5
6
  try {
6
7
  return result.ok(f())
7
8
  } catch (e) {
@@ -12,4 +13,4 @@ const tryCatch = f => {
12
13
  module.exports = {
13
14
  /** @readonly */
14
15
  tryCatch,
15
- }
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.264",
3
+ "version": "0.0.265",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -19,7 +19,9 @@
19
19
  "functional-programming",
20
20
  "closure",
21
21
  "pure-functional",
22
- "typescript"
22
+ "typescript",
23
+ "programming-language",
24
+ "lazy-evaluation"
23
25
  ],
24
26
  "bugs": {
25
27
  "url": "https://github.com/functionalscript/functionalscript/issues"
@@ -1,17 +0,0 @@
1
- const result = require('../../types/result')
2
-
3
- /** @typedef {<T>(req: Require<T>) => (prior: T) => ModuleResult<T>} Module*/
4
-
5
- /**
6
- * @template T
7
- * @typedef {readonly[result.Result<unknown, unknown>, T]} ModuleResult
8
- */
9
-
10
- /**
11
- * @template T
12
- * @typedef {(path: string) => (prior: T) => ModuleResult<T>} Require
13
- */
14
-
15
- /** @typedef {(source: string) => result.Result<Module, unknown>} Compile */
16
-
17
- module.exports = {}