@makano/rew 1.4.2 → 1.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,9 +13,12 @@ const runtime = require("./runtime");
13
13
  const { straceLog } = require("../misc/strace");
14
14
  const reval = require("../functions/reval");
15
15
  const STDNS = require("../const/stdns");
16
+ const emitter = require("../functions/emitter");
16
17
 
17
18
  let mainFile = "";
18
19
  const isMainFile = (filepath) => filepath == mainFile;
20
+ const globalContext = {};
21
+ const globalEmitter = emitter();
19
22
  module.exports.prepareContext = function (
20
23
  custom_context,
21
24
  options,
@@ -42,7 +45,8 @@ module.exports.prepareContext = function (
42
45
  __using__: {},
43
46
  get _(){
44
47
  return void 0;
45
- }
48
+ },
49
+ ...globalContext
46
50
  };
47
51
  if (options.useContext) {
48
52
  context = {
@@ -69,8 +73,11 @@ module.exports.prepareContext = function (
69
73
  this.define = std.prototype.define;
70
74
  this.Main = std.prototype.Main;
71
75
  this.attach = std.prototype.attach;
76
+ this.named = std.prototype.named;
77
+ this.detach = std.prototype.detach;
72
78
  this.out = std.prototype.out;
73
79
  this.inp = std.prototype.in;
80
+ this.signal = std.prototype.signal;
74
81
  this['@cb'] = cb;
75
82
  }
76
83
  });
@@ -94,11 +101,39 @@ module.exports.prepareContext = function (
94
101
  }
95
102
  }
96
103
  return (['main', cb?.main ?? cb]);
97
- }, attach: (object) => {
98
- for(let i in object){
99
- if(!context[i]) context[i] = object[i];
104
+ }, named: (name) => {
105
+ return (item) => [name, item];
106
+ }, attach: (name, object) => {
107
+ if(Array.isArray(name) && name.length == 2 && typeof name[0] == 'string'){
108
+ object = name[1];
109
+ name = name[0];
110
+ }
111
+ if(typeof name == "object" && typeof object == "string"){
112
+ const tname = object;
113
+ object = name;
114
+ name = tname;
100
115
  }
101
- } }
116
+ if(typeof name == "object" && !object){
117
+ object = name;
118
+ name = object.name;
119
+ }
120
+ if(typeof name == "function"){
121
+ object = name;
122
+ name = object.name;
123
+ }
124
+ if(!name) return false;
125
+ globalContext[name] = object;
126
+ globalEmitter.emit('attach_name', name, object);
127
+ return true;
128
+ }, detach: (nameOrObject) => {
129
+ const name = Object.keys(globalContext).find((key) => key == nameOrObject || globalContext[key] == nameOrObject);
130
+ if(name) {
131
+ delete globalContext[name];
132
+ globalEmitter.emit('detach_name', name);
133
+ return true;
134
+ }
135
+ return false;
136
+ }, signal: globalEmitter }
102
137
  Object.defineProperty(std.prototype, 'void', {
103
138
  get: () => void 0
104
139
  })
@@ -208,6 +243,11 @@ module.exports.prepareContext = function (
208
243
  }
209
244
  }
210
245
 
246
+ globalEmitter.on('attach_name', (name, object) => {
247
+ context[name] = object;
248
+ });
249
+ globalEmitter.on('detach_name', (name) => delete context[name]);
250
+
211
251
  if(!context.app){
212
252
  straceLog('==> APP NOT FOUND');
213
253
  context.appPackage = (packageName) => context.app = { config: { manifest: { package: packageName } } }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "A simple coffescript runtime and app manager",
5
5
  "main": "main.js",
6
6
  "directories": {
package/runtime.d.ts CHANGED
@@ -1029,8 +1029,10 @@ declare namespace Rew {
1029
1029
  prototype: {
1030
1030
  void: void,
1031
1031
  Main: () => [string, () => any],
1032
+ named: (name: string) => (item: any) => [string, any],
1032
1033
  define: (name: string, object: any) => any,
1033
- attach: (object: any) => any,
1034
+ attach: (name: string | any, object: any) => any,
1035
+ detach: (nameOrObject: any) => any,
1034
1036
 
1035
1037
  out: stdout;
1036
1038
  in: stdin;