@makano/rew 1.2.96 → 1.2.98

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -12,9 +12,15 @@ Rew
12
12
  <a href="https://www.npmjs.com/package/rayous"> <img src="https://img.shields.io/npm/v/@makano/rew?style=for-the-badge&logo=npm&color=b4befe&logoColor=9399b2&labelColor=181825" alt="npm version" /></a>
13
13
  </p>
14
14
 
15
- Rew is a simple lightweight coffeescript runtime, made to simply using coffescript and revive it
15
+ Rew is a simple lightweight coffeescript runtime, made to simplify using coffescript and revive it
16
16
  in the process.
17
17
 
18
+ ```coffee
19
+ using namespace std::ns ->
20
+ define Main ->
21
+ print 'hello world' |> str
22
+ ```
23
+
18
24
  ## Getting Started
19
25
  1. Install `rew` globally
20
26
  ```bash
@@ -44,6 +44,11 @@ function runFileWithArgv(filePath, options = {}, cargv) {
44
44
  }
45
45
  if (argv.includes('--')) {
46
46
  argv = argv.slice(argv.indexOf('--') + 1, argv.length);
47
+ } else {
48
+ const index = argv.find(p => filePath.endsWith(p.replace(/(\.|\.\.)\//, '/')));
49
+ if(index){
50
+ argv = argv.slice(argv.indexOf(index) + 1, argv.length);
51
+ }
47
52
  }
48
53
  runFile(filePath, options, argv);
49
54
  }
@@ -0,0 +1 @@
1
+ module.exports = class STDNS {}
@@ -1,8 +1,9 @@
1
+ const STDNS = require("./stdns");
1
2
 
2
3
 
3
4
 
4
5
  const JSX_FRAGMENT_SYMBOL = Symbol('fragment');
5
- module.exports.USING_DEFAULT = {
6
+ const USING_DEFAULT = module.exports.USING_DEFAULT = {
6
7
  JSX: {
7
8
  param: (param, fragment) => param ? ({ createElement: param, Fragment: fragment || param(JSX_FRAGMENT_SYMBOL, { isFragment: true }), fragmentSymbol: JSX_FRAGMENT_SYMBOL }) : {},
8
9
  use: (options) => options.jsx = true
@@ -15,7 +16,7 @@ module.exports.USING_DEFAULT = {
15
16
  }
16
17
  }
17
18
 
18
- module.exports.Usage = class Usage {
19
+ class Usage {
19
20
  name = "null";
20
21
  trigger = () => {};
21
22
  save = true;
@@ -61,6 +62,7 @@ module.exports.Usage = class Usage {
61
62
  }
62
63
  }
63
64
  }
65
+ module.exports.Usage = Usage;
64
66
 
65
67
  class Namespace extends module.exports.Usage {
66
68
  namespace = {};
@@ -84,7 +86,44 @@ class Namespace extends module.exports.Usage {
84
86
  module.exports.Namespace = Namespace;
85
87
 
86
88
  module.exports.namespace = (namespace, cb, parent) => {
89
+ if(namespace instanceof STDNS) {
90
+ if(namespace['@cb'] && !cb) {
91
+ cb = namespace['@cb'];
92
+ delete namespace['@cb'];
93
+ }
94
+ }
87
95
  return new Namespace(namespace, cb, parent);
88
96
  }
89
97
 
98
+ module.exports.usingFunction = (context, runtime) => {
99
+ return function using(name, ...params) {
100
+ if(name instanceof Usage.Group){
101
+ params.unshift(...name.g.slice(1));
102
+ name = name.g[0];
103
+ }
104
+ if(USING_DEFAULT[name]){
105
+ if(USING_DEFAULT[name].param) {
106
+ context.__using__[name] = USING_DEFAULT[name].param(...params);
107
+ }
108
+ } else if(name instanceof Namespace) {
109
+ const trigger = name.trigger;
110
+ const parentContext = name.parent || context;
111
+ const childContext = {...parentContext, ...name.namespace, trigger};
112
+ childContext.$self = name.namespace;
113
+ childContext.$parent = parentContext;
114
+ const code = `(${trigger.toString()})()`;
115
+ if(name.onUse) name.onUse();
116
+ const r = runtime.exec(code, childContext, code, context.module.filepath);
117
+ if(name.onAfterUse) name.onAfterUse();
118
+ return r;
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;
123
+ } else {
124
+ context.__using__[name] = params.length ? params.length > 1 ? [...params] : params : true;
125
+ }
126
+ }
127
+ }
128
+
90
129
  module.exports.namespace.group = (group, props) => new Namespace.Group(group, props);
@@ -43,9 +43,10 @@ const lookUpInOtherApps = (fullPath) => {
43
43
  module.exports.imp = function (runPath, context) {
44
44
  return function (filename, options = {}) {
45
45
  if (!options) options = {};
46
+ if(filename == 'std' || filename == '#std') return {};
46
47
  let type = options.type ? options.type : filename.endsWith('.coffee') ? 'coffee' : (
47
48
  filename.endsWith(REW_FILE_TYPE.EXTENSION) ? REW_FILE_TYPE.TYPE :
48
- path.extname(filename).slice(1)
49
+ 'coffee'
49
50
  );
50
51
  let exports,
51
52
  ispkg = findPackage(filename);
@@ -36,7 +36,7 @@ function typedef(value, strict = false) {
36
36
  ? value.constructor
37
37
  : _defaultConstructors[getType(value)],
38
38
  type: getType(value),
39
- isConstucted: typeof value === 'object' && value !== null && !Array.isArray(value),
39
+ isConstructed: typeof value === 'object' && value !== null && !Array.isArray(value),
40
40
  isEmpty: typeof value == 'object' ? !Object.keys(value).length : typeof value == 'string' ? value == '' : typeof value !== 'function',
41
41
  });
42
42
  }
@@ -70,7 +70,7 @@ function typeis(obj, typeDef, missingObjects = false) {
70
70
  // Resolve Type
71
71
  if (typeof typeDef == 'function' && typeDef.type instanceof Type) typeDef = typeDef.type;
72
72
 
73
- if (typeDef.isConstucted && typeDef.class && !(obj instanceof typeDef.class)) {
73
+ if (typeDef.isConstructed && typeDef.class && !(obj instanceof typeDef.class)) {
74
74
  return missingObjects ? [false] : false;
75
75
  }
76
76
 
@@ -285,7 +285,7 @@ function useImp(token, options){
285
285
  let packageName = value.slice(1);
286
286
  token.value = dem+packageName+dem;
287
287
  straceLog('IMP() with HEADER for', packageName);
288
- return includeFile(packageName, options);
288
+ return includeFile(packageName !== 'std' ? packageName : '*'+packageName, options);
289
289
  }
290
290
  return '';
291
291
  }
@@ -378,7 +378,7 @@ function compileRewStuff(content, options) {
378
378
 
379
379
  if (token.type === 'COMMENT' && token.value.slice(1).trim() === '@cls') {
380
380
  options.cls = true;
381
- straceLog('CLITOKENIZATION() ENABLE');
381
+ straceLog('CLI_SYNTAX() ENABLE');
382
382
  straceLog('===> HIGHLY EXPERIMENTAL FEATURE DETECTED');
383
383
  }
384
384
 
@@ -401,6 +401,25 @@ function compileRewStuff(content, options) {
401
401
  token.value = 'pub';
402
402
  straceLog('EXPORT() => TRANSLATING TO pub');
403
403
  }
404
+
405
+ if (token.type === 'IDENTIFIER' && token.value === 'package' && nextToken.type == 'STRING') {
406
+ token.value = 'appPackage';
407
+ straceLog('APP_PACKAGE_CHANGE()');
408
+ }
409
+
410
+ if (
411
+ token.type === 'OTHER' && token.value === '-' &&
412
+ nextToken.type == 'IDENTIFIER' && nextToken.value === 'wait'
413
+ ) {
414
+ const { token: nextNextToken } = gnextToken(i, 2, tokens) || {};
415
+ if(nextNextToken?.type == 'IDENTIFIER'){
416
+ token.value = '';
417
+ hooks.push({
418
+ index: i + 3,
419
+ value: ','
420
+ });
421
+ }
422
+ }
404
423
 
405
424
  if (token.type === 'IDENTIFIER' && token.value === 'using' && !options.disableUse) {
406
425
  straceLog('USING()');
@@ -432,15 +451,15 @@ function compileRewStuff(content, options) {
432
451
  if (token.type === 'IDENTIFIER' && token.value === 'import' && !options.keepImports) {
433
452
  // console.log(nextToken.type);
434
453
  straceLog('IMPORT()');
435
- straceLog('==> WARN: SLOWS DOWN TOKENIZATION');
454
+ straceLog('==> WARN: SLOWS DOWN COMPILATION');
436
455
  let ind = i + n + 2;
437
456
  let isAs = false;
438
457
 
439
458
  let defaultName;
440
459
  if (nextToken.type === 'STRING') {
441
460
  straceLog('==> SIMPLE');
461
+ if(useImp(nextToken, options)) updateAliases(aliases);
442
462
  result += `inc ${nextToken.value}`;
443
- if(useImp(nameToken, options)) updateAliases(aliases);
444
463
  i += n;
445
464
  } else if (nextToken.value === '{') {
446
465
  const closingBraceToken = fnextToken(ind, tokens, 'OTHER', '}');
@@ -8,11 +8,11 @@ const pathLib = require("../functions/path");
8
8
  const path = require("path");
9
9
  const execLib = require("../functions/exec");
10
10
  const { findAppInfo } = require("../misc/findAppInfo");
11
- const { USING_DEFAULT, Usage, Namespace } = require("../const/usage");
11
+ const { Usage, usingFunction } = require("../const/usage");
12
12
  const runtime = require("./runtime");
13
- const { permission } = require("process");
14
13
  const { straceLog } = require("../misc/strace");
15
14
  const reval = require("../functions/reval");
15
+ const STDNS = require("../const/stdns");
16
16
 
17
17
  let mainFile = "";
18
18
  const isMainFile = (filepath) => filepath == mainFile;
@@ -47,14 +47,41 @@ module.exports.prepareContext = function (
47
47
  ...context,
48
48
  };
49
49
  } else {
50
+ const std = {...defaultContext};
50
51
  context = {
51
52
  ...context,
52
- ...defaultContext,
53
+ ...std,
53
54
  ...pathLib(filepath),
54
55
  ...execLib(filepath),
55
56
  ...custom_context,
56
57
  Usage
57
58
  };
59
+ std.prototype = { ns: (cb) => {
60
+ return new (class extends STDNS {
61
+ constructor(){
62
+ super();
63
+ for(let i in std){
64
+ this[i] = std[i];
65
+ }
66
+ this.define = std.prototype.define;
67
+ this.Main = std.prototype.Main;
68
+ this['@cb'] = cb;
69
+ }
70
+ });
71
+ }, define: (name, object) => {
72
+ if(Array.isArray(name) && name.length == 2 && typeof name[0] == 'string'){
73
+ object = name[1];
74
+ name = name[0];
75
+ }
76
+ if(!context.module.exports) context.module.exports = {};
77
+ context.module.exports[name] = object;
78
+ context[name] = object;
79
+ }, Main: (cb) => (['main', cb?.main ?? cb]), attach: (object) => {
80
+ for(let i in object){
81
+ if(!context[i]) context[i] = object[i];
82
+ }
83
+ } }
84
+ context.std = std;
58
85
  }
59
86
  if (!context.process)
60
87
  context.process = {
@@ -70,10 +97,19 @@ module.exports.prepareContext = function (
70
97
  abort: () => process.abort(),
71
98
  kill: () => process.kill(),
72
99
  exit: (code) => process.exit(code),
100
+ chdir: (dir) => process.chdir(dir),
101
+ disconnect: () => process.disconnect(),
73
102
  arch: process.arch,
74
103
  pid: process.pid,
75
104
  platform: process.platform,
76
- permission: process.permission
105
+ channel: process.channel,
106
+ uptime: () => process.uptime(),
107
+ nextTick: (callback, ...args) => process.nextTick(callback, ...args),
108
+ permission: process.permission,
109
+ transmit: {
110
+ send: (...data) => process.send(...data),
111
+ recieve: (cb) => process.on('message', cb)
112
+ }
77
113
  };
78
114
 
79
115
  context.global = context;
@@ -120,34 +156,7 @@ module.exports.prepareContext = function (
120
156
  context.pub = pubFunction(context);
121
157
  context.exports = exportsFunction(context);
122
158
 
123
- context.using = (name, ...params) => {
124
- if(name instanceof Usage.Group){
125
- params.unshift(...name.g.slice(1));
126
- name = name.g[0];
127
- }
128
- if(USING_DEFAULT[name]){
129
- if(USING_DEFAULT[name].param) {
130
- context.__using__[name] = USING_DEFAULT[name].param(...params);
131
- }
132
- } else if(name instanceof Namespace) {
133
- const trigger = name.trigger;
134
- const parentContext = name.parent || context;
135
- const childContext = {...parentContext, ...name.namespace, trigger};
136
- childContext.currentNamespace = name.namespace;
137
- childContext.parentNamespace = parentContext;
138
- const code = `(${trigger.toString()})()`;
139
- if(name.onUse) name.onUse();
140
- const r = runtime.exec(code, childContext, code, context.module.filepath);
141
- if(name.onAfterUse) name.onAfterUse();
142
- return r;
143
- } else if(name instanceof Usage) {
144
- const v = name.trigger(...params) || true;
145
- if(name.save !== false) context.__using__[name.name] = v;
146
- return v;
147
- } else {
148
- context.__using__[name] = params.length ? params.length > 1 ? [...params] : params : true;
149
- }
150
- };
159
+ context.using = usingFunction(context, runtime);
151
160
 
152
161
  if(context.app?.config?.exec?.['auto import']){
153
162
  const autoipath = path.join(context.app.path, context.app.config?.exec?.['auto import']);
@@ -54,7 +54,7 @@ module.exports.runPath = function runPath(filepath, options = {}, custom_context
54
54
  const mainFn = context.module.exports.main ?? context.module.exports;
55
55
  return {
56
56
  context,
57
- returns: mainFn(context.process.argv)
57
+ returns: mainFn.call(context, context.process.argv)
58
58
  }
59
59
  } else {
60
60
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.2.96",
3
+ "version": "1.2.98",
4
4
  "description": "A simple coffescript runtime and app manager",
5
5
  "main": "main.js",
6
6
  "directories": {