@makano/rew 1.3.8 → 1.4.0

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.
@@ -22,9 +22,9 @@ class Usage {
22
22
  save = true;
23
23
 
24
24
  constructor(name, trigger, save){
25
- this.name = name;
25
+ this.name = name || '';
26
26
  this.trigger = trigger;
27
- this.save = name == '' ? false : save;
27
+ this.save = name == '' || name == null ? false : save;
28
28
  }
29
29
 
30
30
  create(name, trigger, save = true){
@@ -117,9 +117,9 @@ module.exports.usingFunction = (context, runtime) => {
117
117
  if(name.onAfterUse) name.onAfterUse();
118
118
  return r;
119
119
  } else if(name instanceof Usage) {
120
- const v = name.trigger(...params) || true;
121
- if(name.save !== false) context.__using__[name.name] = v;
122
- return v;
120
+ const v = name.trigger(...params);
121
+ if(name.save !== false) context.__using__[name.name] = v ?? true;
122
+ return v || (typeof name.result === "function" ? name.result(v) : name.result || true);
123
123
  } else {
124
124
  context.__using__[name] = params.length ? params.length > 1 ? [...params] : params : true;
125
125
  }
@@ -1,6 +1,6 @@
1
1
  const { spawnSync } = require('child_process');
2
2
 
3
- const print = (module.exports.print = function print(...args) {
3
+ (module.exports.print = function print(...args) {
4
4
  return console.log(...args);
5
5
  });
6
6
 
@@ -8,9 +8,6 @@ module.exports.printf = function printf(buffer, cb) {
8
8
  return process.stdout.write(buffer, cb);
9
9
  };
10
10
 
11
- print.stdout = process.stdout;
12
- print.stdin = process.stdin;
13
-
14
11
  module.exports.input = function input(prompt) {
15
12
  process.stdout.write(prompt);
16
13
 
@@ -154,11 +154,15 @@ function declareAlias(aliases, token) {
154
154
 
155
155
  if(name.startsWith('=')){
156
156
  name = name.slice(1);
157
- let isDecOnly = false;
157
+ let isDecOnly = false, isConstructor = false;
158
158
  if(name.endsWith('*')) {
159
159
  name = name.slice(0, -1);
160
160
  isDecOnly = true;
161
161
  }
162
+ if(name.endsWith('!')) {
163
+ name = name.slice(0, -1);
164
+ isConstructor = true;
165
+ }
162
166
  aliasValue = (token, tokens, code, hooks, index, setIndex) => {
163
167
  const nextToken = tokens[index+1]
164
168
  let nextToken2 = gnextToken(index, 3, tokens);
@@ -181,7 +185,7 @@ function declareAlias(aliases, token) {
181
185
  let nextToken = gnextToken(index, offset+1, tokens);
182
186
  const args = nextToken.token.value;
183
187
  setIndex(ti + offset);
184
- return `${nextToken2.value} = ${token.value} ${args && args !== '(' && args !== '{' && args !== '[' && args !== '-' && args !== '=' ? `${args},` : ''} ${params.trim() ? params.trim() + ', ' : ''}${args == '(' || args == '[' || args == '{' || args == '=' || args == '-' ? args : ''}`.replace(/,(| )$/, '');
188
+ return `${nextToken2.value} = ${isConstructor?'new ':''}${token.value} ${args && args !== '(' && args !== '{' && args !== '[' && args !== '-' && args !== '=' ? `${args},` : ''} ${params.trim() ? params.trim() + ', ' : ''}${args == '(' || args == '[' || args == '{' || args == '=' || args == '-' ? args : ''}`.replace(/,(| )$/, '');
185
189
  } else if(nextToken?.value == ' ' && (isDecOnly || nextToken2?.token.value == '=' || nextToken2?.token.value == ':')){
186
190
  nextToken.value = '';
187
191
  if(isDecOnly){
@@ -194,7 +198,7 @@ function declareAlias(aliases, token) {
194
198
  if(nextToken2.token.value == ':') nextToken2.token.value = '=';
195
199
  hooks.push({
196
200
  index: nextToken2.ti,
197
- value: ' ' + value
201
+ value: ' ' + (isConstructor?'new ':'') + value
198
202
  })
199
203
  return "";
200
204
  }
@@ -215,8 +219,8 @@ function declareAlias(aliases, token) {
215
219
 
216
220
  const stdTypes = (isPublic) => {
217
221
  let r = '';
218
- const dec = (name, fn) => {
219
- r += `#declare${isPublic?'*':''} key "=${name}" = ${fn || name};\n`
222
+ const dec = (name, fn, cons = 0) => {
223
+ r += `#declare${isPublic?'*':''} key "=${name}${cons ? '!' : ''}" = ${fn || name};\n`
220
224
  }
221
225
  dec('int');
222
226
  dec('str');
@@ -226,6 +230,7 @@ const stdTypes = (isPublic) => {
226
230
  dec('typedef');
227
231
  dec('typef');
228
232
  dec('struct');
233
+ dec('Usage', null, 1);
229
234
  return r;
230
235
  };
231
236
  const includeFile = (includeContent, options) => {
@@ -461,7 +466,7 @@ function compileRewStuff(content, options) {
461
466
 
462
467
  if (tokens[i-1]?.value !== '.' && token.type === 'IDENTIFIER' && token.value === 'using' && !options.disableUse) {
463
468
  straceLog('USING()');
464
- const next = nextToken.value;
469
+ const next = nextToken?.value;
465
470
  if(next in USING_DEFAULT) {
466
471
  const { use } = USING_DEFAULT[next];
467
472
  use?.(options);
@@ -694,7 +699,15 @@ const compileCivetStuff = (file, options) => {
694
699
  filename: file.path,
695
700
  ...options
696
701
  };
697
- straceLog('OPTION_PREPARE() for CURRENTFILE as', preCompileOptions);
702
+ straceLog('OPTION_PREPARE() for CURRENTFILE as', JSON.stringify(preCompileOptions));
703
+
704
+ if(options?.type == 'js' || file?.path?.endsWith('.js')){
705
+ return {
706
+ compiled: file?.content || "",
707
+ options: preCompileOptions
708
+ }
709
+ }
710
+
698
711
  const prepared = compileRewStuff(file.content, preCompileOptions);
699
712
 
700
713
  // console.log(prepared);
@@ -65,10 +65,13 @@ module.exports.prepareContext = function (
65
65
  }
66
66
  this.define = std.prototype.define;
67
67
  this.Main = std.prototype.Main;
68
+ this.attach = std.prototype.attach;
69
+ this.out = std.prototype.out;
70
+ this.inp = std.prototype.in;
68
71
  this['@cb'] = cb;
69
72
  }
70
73
  });
71
- }, define: (name, object) => {
74
+ }, out: {...process.stdout, put: (...logs) => context.print(...logs), strace: (...logs) => straceLog('==> STRACE_OUT():', ...logs ), write: (logs) => context.printf(logs+'\n') }, in: {...process.stdin, read: (...args) => context.input(...args)}, define: (name, object) => {
72
75
  if(Array.isArray(name) && name.length == 2 && typeof name[0] == 'string'){
73
76
  object = name[1];
74
77
  name = name[0];
@@ -127,6 +130,26 @@ module.exports.prepareContext = function (
127
130
  }
128
131
  };
129
132
 
133
+ context.std.prototype.__ = {
134
+ get prototype(){
135
+ const globals = context.app?.config?.assets?.globals ?? '_';
136
+ const p = context.pjoin(context.app ? context.app.path : context.dirname(context.module.filename), globals);
137
+ if(!context.exists(p)) return {};
138
+ const files = context.ls(p);
139
+ const pr = {};
140
+ files.forEach(file => {
141
+ if(!file.endsWith('.coffee')) return;
142
+ const e = runPath(context.pjoin(p, file));
143
+ let name = e.context.module.modset || file.split('.').shift();
144
+ pr[name] = e.context.module.exports || {};
145
+ if(typeof pr[name] == "object" && pr[name].default && Object.keys(pr[name]).length == 1){
146
+ pr[name] = pr[name].default;
147
+ }
148
+ });
149
+ return pr;
150
+ }
151
+ }
152
+
130
153
  context.global = context;
131
154
  context.imports.assert = options.import ?? {};
132
155
  context.imp = imp(runPath, context);
@@ -185,6 +208,8 @@ module.exports.prepareContext = function (
185
208
  if(!context.app){
186
209
  straceLog('==> APP NOT FOUND');
187
210
  context.appPackage = (packageName) => context.app = { config: { manifest: { package: packageName } } }
211
+ } else {
212
+ context.appPackage = context.mod = (packageName) => context.module.modset = packageName;
188
213
  }
189
214
 
190
215
  Object.defineProperty(context, 'packageName', {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.3.8",
3
+ "version": "1.4.0",
4
4
  "description": "A simple coffescript runtime and app manager",
5
5
  "main": "main.js",
6
6
  "directories": {
package/runtime.d.ts CHANGED
@@ -958,13 +958,7 @@ declare namespace Rew {
958
958
  ): ReturnType<typeof future>;
959
959
 
960
960
  declare function print(...args: any[]): void;
961
- declare namespace print {
962
- // @ts-ignore
963
- const stdout: WriteStream;
964
- // @ts-ignore
965
- const stdin: ReadStream;
966
- }
967
-
961
+ declare function printf(...args: any[]): void;
968
962
  declare function input(prompt: string): string;
969
963
 
970
964
  declare const basename: (path: string) => string;
@@ -1009,6 +1003,18 @@ declare namespace Rew {
1009
1003
  group(...group: any[]): { g: T, with: (props: any) => { g: T, [key: string]: any }, [key: string]: any }
1010
1004
  }
1011
1005
 
1006
+ // @ts-ignore
1007
+ interface stdout extends WriteStream {
1008
+ put: typeof print;
1009
+ write: (str: string) => void;
1010
+ strace: (...str: string[]) => void;
1011
+ }
1012
+
1013
+ // @ts-ignore
1014
+ interface stdin extends ReadStream {
1015
+ read: typeof input
1016
+ }
1017
+
1012
1018
  declare const std: {
1013
1019
  curl: typeof curl,
1014
1020
  int: typeof int,
@@ -1021,11 +1027,17 @@ declare namespace Rew {
1021
1027
  typei: typeof typei,
1022
1028
 
1023
1029
  prototype: {
1024
- void: () => void 0,
1030
+ void: void,
1025
1031
  Main: () => [string, () => any],
1026
1032
  define: (name: string, object: any) => any,
1027
1033
  attach: (object: any) => any,
1028
- ns: () => any
1034
+
1035
+ out: stdout;
1036
+ in: stdin;
1037
+
1038
+ ns: () => any;
1039
+
1040
+ __: Record<string, any>;
1029
1041
  }
1030
1042
  }
1031
1043