summary refs log blame commit diff stats
path: root/HACKING
blob: 85f44ed684ac00ea299fbc75e9009cc8f68d84f0 (plain) (tree)


















                                                                          
                      

                                                                       
                                                            


















                                                            
                           

                              
                              



                                                                      


                                                                              








                                                                 
                                                                       





















                                                                      
Guidelines on Code Modification
===============================

Coding Style
------------

* Use syntax compatible to both python 2.6 and 3.1.
* Use docstrings with pydoc in mind
* Follow the style guide for python code:
    http://www.python.org/dev/peps/pep-0008/
* Although this guide suggests otherwise, tabs are used for indentation
    of code and docstrings.  In other documents (readme, etc), use spaces.


Patches
-------

Send patches, created with "git format-patch", to the email adress

    romanz@lavabit.com

If you plan to do major changes, or many changes over time, I encourage
you to create a fork on GitHub, Gitorious or any other site.


Common Changes
--------------

* Change which files are previewed in the auto preview:
In ranger/gui/widget/browsercolumn.py
the constant PREVIEW_BLACKLIST

* Adding options:
In ranger/defaults/options.py
add the default value, like: my_option = True
In ranger/shared/settings.py
add the name of your option to the constant ALLOWED_SETTINGS

The setting is now accessible at self.settings.my_option,
assuming <self> is a "SettingsAware" object.

* Change commands:
ranger/defaults/commands.py

* Create aliases for commands:
In ranger/defaults/commands.py
at the bottom, write something like: alias(exit=quit)

* Adding colorschemes:
Copy ranger/colorschemes/default.py to ranger/colorschemes/myscheme.py
and modify it according to your needs.  Alternatively, mimic the jungle
colorscheme.  It subclasses the default scheme and just modifies a few things.
In ranger/defaults/options.py (or ~/.ranger/options.py), change
    colorscheme = colorschemes.default
to: colorscheme = colorschemes.myscheme

* Change which files are considered to be "hidden":
In ranger/defaults/options.py
change the hidden_filter regular expression.

* Change the key map:
Modify ranger/defaults/keys.py.  This should be self-explanatory.
Check out ranger/core/actions.py for the most common actions, of course
you can also use your own functions.

* Change the file type => application associations:
In ranger/defaults/apps.py
modify the method app_default.
The variable "f" is a filesystem-object with attributes like mimetype,
extension, etc.  For a full list, check ranger/fsobject/fsobject.py

* Change the file extension => mime type associations:
Modify ranger/data/mime.types
and run ranger/data/generate.py to compile it.


Version Numbering
-----------------

X.Y.Z, where:

* X: Milestones
* Y: Stable versions
* Z: Experimental versions
0e25ada0f0f2a390b12e981'>^
1422f339 ^


bb2d3724 ^
d3a32a80 ^



1422f339 ^
d3a32a80 ^



d5bb0eac ^
d3a32a80 ^
bb2d3724 ^
1422f339 ^





1422f339 ^
d5bb0eac ^
06326cdd ^
d5bb0eac ^


1422f339 ^



06326cdd ^
























1422f339 ^

































































3a7d189d ^


1422f339 ^
69870f3b ^









18008acc ^


12aa7f1a ^







1422f339 ^








1422f339 ^
d5bb0eac ^
06326cdd ^
d5bb0eac ^


1422f339 ^










1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429














                                                                                
                                                          
























                                                                                



                       
                 

                 
                










                                                         
                                               




                                                            
                                                 
                                             






                                                                         


                                                        
                                                 










                                                                              
                                                                             



                                                                              










                                                                                
                 
 








                                                                  




                                                                           



                                                                  







                                                                             




                                                                      



                                                                       











                                                                         




























                                                      

                                                 


























                                                             
                       












                                                         







                                                   


                                                                        
 













                                                         

                                                          


                                                           
                                 



                                                    
                          



                                                                              
                 
                                          
                               





                                                           
                          
                      
                                                      


               



                                                      
























                                                                               

































































                                                                               


                                                      
 









                                                                           


                                                                         







                                                   








                                                                            
                            
                      
                                                      


                  










                                                                            
# Automatic conversion of Nim types to JavaScript types.
#
# Every conversion involves copying unless explicitly noted below.
#
# * Primitives are converted to their respective JavaScript counterparts.
# * seq is converted to a JS array. Note: this always copies the seq's contents.
# * Promise is converted to a JS promise which will be resolved when the Nim
#   promise is resolved.
# * enum is converted to its stringifier's output.
# * JSValue is returned as-is, *without* a DupValue operation.
# * JSError is converted to a new error object corresponding to the error
#   it represents.
# * JSArrayBuffer, JSUint8Array are converted to a JS object without copying
#   their contents.
# * NarrowString is converted to a JS narrow string (with copying). For more
#   information on JS string handling, see js/jstypes.nim.
# * Finally, ref object is converted to a JS object whose opaque is the ref
#   object. (See below.)
#
# Note that ref objects can be seamlessly converted to JS objects, despite
# the fact that they are managed by two separate garbage collectors. This
# works thanks to a patch in QJS and machine oil. Basically:
#
# * Nim objects registered with registerType can be paired with one (1)
#   JS object each.
# * This happens on-demand, whenever the Nim object has to be converted into JS.
# * Once the conversion happened, the JS object will be kept alive until the
#   Nim object is destroyed, so that JS properties on the JS object are not
#   lost during a re-conversion.
# * Similarly, the Nim object is kept alive so long as the JS object is alive.
# * The patched in can_destroy hook is used to synchronize reference counts
#   of the two objects; this way, no memory leak occurs.
#
# There are also toJSP variants of object converters. These work identically
# to ref object converters, except the reference count of the closest
# `ref object' ancestor is incremented/decremented when synchronizing refcounts
# with the JS object pair.

import std/options
import std/tables
import std/unicode

import bindings/quickjs
import io/promise
import js/error
import js/jstypes
import js/opaque
import js/typeptr
import types/opt

# Convert Nim types to the corresponding JavaScript type.
# This does not work with var objects.
proc toJS*(ctx: JSContext, s: string): JSValue
proc toJS*(ctx: JSContext, r: Rune): JSValue
proc toJS*(ctx: JSContext, n: int64): JSValue
proc toJS*(ctx: JSContext, n: int32): JSValue
proc toJS*(ctx: JSContext, n: int): JSValue
proc toJS*(ctx: JSContext, n: uint16): JSValue
proc toJS*(ctx: JSContext, n: uint32): JSValue
proc toJS*(ctx: JSContext, n: uint64): JSValue
proc toJS*(ctx: JSContext, n: float64): JSValue
proc toJS*(ctx: JSContext, b: bool): JSValue
proc toJS*[U, V](ctx: JSContext, t: Table[U, V]): JSValue
proc toJS*(ctx: JSContext, opt: Option): JSValue
proc toJS*[T, E](ctx: JSContext, opt: Result[T, E]): JSValue
proc toJS*(ctx: JSContext, s: seq): JSValue
proc toJS*[T](ctx: JSContext, s: set[T]): JSValue
proc toJS*(ctx: JSContext, t: tuple): JSValue
proc toJS*(ctx: JSContext, e: enum): JSValue
proc toJS*(ctx: JSContext, j: JSValue): JSValue
proc toJS*[T](ctx: JSContext, promise: Promise[T]): JSValue
proc toJS*[T, E](ctx: JSContext, promise: Promise[Result[T, E]]): JSValue
proc toJS*(ctx: JSContext, promise: EmptyPromise): JSValue
proc toJS*(ctx: JSContext, obj: ref object): JSValue
proc toJS*(ctx: JSContext, err: JSError): JSValue
proc toJS*(ctx: JSContext, abuf: JSArrayBuffer): JSValue
proc toJS*(ctx: JSContext, u8a: JSUint8Array): JSValue
proc toJS*(ctx: JSContext, ns: NarrowString): JSValue
proc toJS*(ctx: JSContext, dict: JSDict): JSValue

# Convert Nim types to the corresponding JavaScript type, with knowledge of
# the parent object.
# This supports conversion of var object types.
#
# The idea here is to allow conversion of var objects to quasi-reference types
# by saving a pointer to their ancestor and incrementing/decrementing the
# ancestor's reference count instead.
proc toJSP*(ctx: JSContext, parent: ref object, child: var object): JSValue
proc toJSP*(ctx: JSContext, parent: ptr object, child: var object): JSValue

# Same as toJS, but used in constructors. ctor contains the target prototype,
# used for subclassing from JS.
proc toJSNew*(ctx: JSContext, obj: ref object, ctor: JSValue): JSValue
proc toJSNew*[T, E](ctx: JSContext, opt: Result[T, E], ctor: JSValue): JSValue

# Avoid accidentally calling toJSP on objects that we have explicit toJS
# converters for.
template makeToJSP(typ: untyped) =
  template toJSP*(ctx: JSContext, parent: ref object, child: var typ): JSValue =
    toJS(ctx, child)
  template toJSP*(ctx: JSContext, parent: ptr object, child: var typ): JSValue =
    toJS(ctx, child)
makeToJSP(Table)
makeToJSP(Option)
makeToJSP(Result)
makeToJSP(JSValue)
makeToJSP(JSDict)

proc defineProperty(ctx: JSContext, this: JSValue, name: JSAtom,
    prop: JSValue, flags = cint(0)) =
  if JS_DefinePropertyValue(ctx, this, name, prop, flags) <= 0:
    raise newException(Defect, "Failed to define property string")

proc definePropertyC*(ctx: JSContext, this: JSValue, name: JSAtom,
    prop: JSValue) =
  ctx.defineProperty(this, name, prop, JS_PROP_CONFIGURABLE)

proc defineProperty(ctx: JSContext, this: JSValue, name: string,
    prop: JSValue, flags = cint(0)) =
  if JS_DefinePropertyValueStr(ctx, this, cstring(name), prop, flags) <= 0:
    raise newException(Defect, "Failed to define property string: " & name)

proc definePropertyC*(ctx: JSContext, this: JSValue, name: string,
    prop: JSValue) =
  ctx.defineProperty(this, name, prop, JS_PROP_CONFIGURABLE)

proc defineProperty*[T](ctx: JSContext, this: JSValue, name: string, prop: T,
    flags = cint(0)) =
  defineProperty(ctx, this, name, toJS(ctx, prop), flags)

proc definePropertyE*[T](ctx: JSContext, this: JSValue, name: string,
    prop: T) =
  defineProperty(ctx, this, name, prop, JS_PROP_ENUMERABLE)

proc definePropertyCW*[T](ctx: JSContext, this: JSValue, name: string,
    prop: T) =
  defineProperty(ctx, this, name, prop, JS_PROP_CONFIGURABLE or
    JS_PROP_WRITABLE)

proc definePropertyCWE*[T](ctx: JSContext, this: JSValue, name: string,
    prop: T) =
  defineProperty(ctx, this, name, prop, JS_PROP_C_W_E)

proc newFunction*(ctx: JSContext, args: openArray[string], body: string):
    JSValue =
  var paramList: seq[JSValue] = @[]
  for arg in args:
    paramList.add(toJS(ctx, arg))
  paramList.add(toJS(ctx, body))
  let fun = JS_CallConstructor(ctx, ctx.getOpaque().Function_ctor,
    cint(paramList.len), addr paramList[0])
  for param in paramList:
    JS_FreeValue(ctx, param)
  return fun

proc toJS*(ctx: JSContext, s: cstring): JSValue =
  return JS_NewString(ctx, s)

proc toJS*(ctx: JSContext, s: string): JSValue =
  return toJS(ctx, cstring(s))

proc toJS*(ctx: JSContext, r: Rune): JSValue =
  return toJS(ctx, $r)

proc toJS*(ctx: JSContext, n: int32): JSValue =
  return JS_NewInt32(ctx, n)

proc toJS*(ctx: JSContext, n: int64): JSValue =
  return JS_NewInt64(ctx, n)

# Always int32, so we don't risk 32-bit only breakage.
proc toJS*(ctx: JSContext, n: int): JSValue =
  return toJS(ctx, int32(n))

proc toJS*(ctx: JSContext, n: uint16): JSValue =
  return JS_NewUint32(ctx, uint32(n))

proc toJS*(ctx: JSContext, n: uint32): JSValue =
  return JS_NewUint32(ctx, n)

proc toJS*(ctx: JSContext, n: uint64): JSValue =
  #TODO this is incorrect
  return JS_NewFloat64(ctx, float64(n))

proc toJS*(ctx: JSContext, n: float64): JSValue =
  return JS_NewFloat64(ctx, n)

proc toJS*(ctx: JSContext, b: bool): JSValue =
  return JS_NewBool(ctx, b)

proc toJS*[U, V](ctx: JSContext, t: Table[U, V]): JSValue =
  let obj = JS_NewObject(ctx)
  if not JS_IsException(obj):
    for k, v in t:
      definePropertyCWE(ctx, obj, k, v)
  return obj

proc toJS*(ctx: JSContext, opt: Option): JSValue =
  if opt.isSome:
    return toJS(ctx, opt.get)
  return JS_NULL

proc toJS[T, E](ctx: JSContext, opt: Result[T, E]): JSValue =
  if opt.isSome:
    when not (T is void):
      return toJS(ctx, opt.get)
    else:
      return JS_UNDEFINED
  else:
    when not (E is void):
      let res = toJS(ctx, opt.error)
      if not JS_IsNull(res):
        return JS_Throw(ctx, res)
    return JS_EXCEPTION

proc toJS(ctx: JSContext, s: seq): JSValue =
  let a = JS_NewArray(ctx)
  if not JS_IsException(a):
    for i in 0..s.high:
      let j = toJS(ctx, s[i])
      if JS_IsException(j):
        return j
      if JS_DefinePropertyValueInt64(ctx, a, int64(i), j,
          JS_PROP_C_W_E or JS_PROP_THROW) < 0:
        return JS_EXCEPTION
  return a

proc toJS*[T](ctx: JSContext, s: set[T]): JSValue =
  #TODO this is a bit lazy :p
  var x = newSeq[T]()
  for e in s:
    x.add(e)
  var a = toJS(ctx, x)
  if JS_IsException(a):
    return a
  let ret = JS_CallConstructor(ctx, ctx.getOpaque().Set_ctor, 1, addr a)
  JS_FreeValue(ctx, a)
  return ret

proc toJS(ctx: JSContext, t: tuple): JSValue =
  let a = JS_NewArray(ctx)
  if not JS_IsException(a):
    var i = 0
    for f in t.fields:
      let j = toJS(ctx, f)
      if JS_IsException(j):
        return j
      if JS_DefinePropertyValueInt64(ctx, a, int64(i), j,
          JS_PROP_C_W_E or JS_PROP_THROW) < 0:
        return JS_EXCEPTION
      inc i
  return a

proc toJSP0(ctx: JSContext, p, tp: pointer, ctor: JSValue,
    needsref: var bool): JSValue =
  JS_GetRuntime(ctx).getOpaque().plist.withValue(p, obj):
    # a JSValue already points to this object.
    return JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, obj[]))
  let ctxOpaque = ctx.getOpaque()
  let class = ctxOpaque.typemap[tp]
  let jsObj = JS_NewObjectFromCtor(ctx, ctor, class)
  if JS_IsException(jsObj):
    return jsObj
  setOpaque(ctx, jsObj, p)
  # We are constructing a new JS object, so we must add unforgeable properties
  # here.
  ctxOpaque.unforgeable.withValue(class, uf):
    JS_SetPropertyFunctionList(ctx, jsObj, addr uf[][0], cint(uf[].len))
  needsref = true
  if unlikely(ctxOpaque.htmldda == class):
    JS_SetIsHTMLDDA(ctx, jsObj)
  return jsObj

proc toJSRefObj(ctx: JSContext, obj: ref object): JSValue =
  if obj == nil:
    return JS_NULL
  let p = cast[pointer](obj)
  let tp = getTypePtr(obj)
  var needsref = false
  let val = toJSP0(ctx, p, tp, JS_UNDEFINED, needsref)
  if needsref:
    GC_ref(obj)
  return val

proc toJS*(ctx: JSContext, obj: ref object): JSValue =
  return toJSRefObj(ctx, obj)

proc toJSNew*(ctx: JSContext, obj: ref object, ctor: JSValue): JSValue =
  if obj == nil:
    return JS_NULL
  let p = cast[pointer](obj)
  let tp = getTypePtr(obj)
  var needsref = false
  let val = toJSP0(ctx, p, tp, ctor, needsref)
  if needsref:
    GC_ref(obj)
  return val

proc toJSNew[T, E](ctx: JSContext, opt: Result[T, E], ctor: JSValue): JSValue =
  if opt.isSome:
    when not (T is void):
      return toJSNew(ctx, opt.get, ctor)
    else:
      return JS_UNDEFINED
  else:
    when not (E is void):
      let res = toJS(ctx, opt.error)
      if not JS_IsNull(res):
        return JS_Throw(ctx, res)
    else:
      return JS_NULL

proc toJS(ctx: JSContext, e: enum): JSValue =
  return toJS(ctx, $e)

proc toJS(ctx: JSContext, j: JSValue): JSValue =
  return j

proc toJS(ctx: JSContext, promise: EmptyPromise): JSValue =
  var resolving_funcs: array[2, JSValue]
  let jsPromise = JS_NewPromiseCapability(ctx, addr resolving_funcs[0])
  if JS_IsException(jsPromise):
    return JS_EXCEPTION
  promise.then(proc() =
    var x = JS_UNDEFINED
    let res = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED, 1, addr x)
    JS_FreeValue(ctx, res)
    JS_FreeValue(ctx, resolving_funcs[0])
    JS_FreeValue(ctx, resolving_funcs[1]))
  return jsPromise

proc toJS[T](ctx: JSContext, promise: Promise[T]): JSValue =
  var resolving_funcs: array[2, JSValue]
  let jsPromise = JS_NewPromiseCapability(ctx, addr resolving_funcs[0])
  if JS_IsException(jsPromise):
    return JS_EXCEPTION
  promise.then(proc(x: T) =
    var x = toJS(ctx, x)
    let res = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED, 1, addr x)
    JS_FreeValue(ctx, res)
    JS_FreeValue(ctx, x)
    JS_FreeValue(ctx, resolving_funcs[0])
    JS_FreeValue(ctx, resolving_funcs[1]))
  return jsPromise

proc toJS[T, E](ctx: JSContext, promise: Promise[Result[T, E]]): JSValue =
  var resolving_funcs: array[2, JSValue]
  let jsPromise = JS_NewPromiseCapability(ctx, addr resolving_funcs[0])
  if JS_IsException(jsPromise):
    return JS_EXCEPTION
  promise.then(proc(x: Result[T, E]) =
    if x.isOk:
      let x = when T is void:
        JS_UNDEFINED
      else:
        toJS(ctx, x.get)
      let res = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED, 1, unsafeAddr x)
      JS_FreeValue(ctx, res)
      JS_FreeValue(ctx, x)
    else: # err
      let x = when E is void:
        JS_UNDEFINED
      else:
        toJS(ctx, x.error)
      let res = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED, 1, unsafeAddr x)
      JS_FreeValue(ctx, res)
      JS_FreeValue(ctx, x)
    JS_FreeValue(ctx, resolving_funcs[0])
    JS_FreeValue(ctx, resolving_funcs[1]))
  return jsPromise

proc toJS*(ctx: JSContext, err: JSError): JSValue =
  if err.e notin QuickJSErrors:
    return toJSRefObj(ctx, err)
  var msg = toJS(ctx, err.message)
  if JS_IsException(msg):
    return msg
  let ctor = ctx.getOpaque().err_ctors[err.e]
  let ret = JS_CallConstructor(ctx, ctor, 1, addr msg)
  JS_FreeValue(ctx, msg)
  return ret

proc toJS*(ctx: JSContext, abuf: JSArrayBuffer): JSValue =
  return JS_NewArrayBuffer(ctx, abuf.p, abuf.len, abuf.dealloc, nil, false)

proc toJS*(ctx: JSContext, u8a: JSUint8Array): JSValue =
  var jsabuf = toJS(ctx, u8a.abuf)
  let ctor = ctx.getOpaque().Uint8Array_ctor
  let ret = JS_CallConstructor(ctx, ctor, 1, addr jsabuf)
  JS_FreeValue(ctx, jsabuf)
  return ret

proc toJS*(ctx: JSContext, ns: NarrowString): JSValue =
  return JS_NewNarrowStringLen(ctx, cstring(ns), csize_t(string(ns).len))

proc toJS*(ctx: JSContext, dict: JSDict): JSValue =
  let obj = JS_NewObject(ctx)
  if JS_IsException(obj):
    return obj
  for k, v in dict.fieldPairs:
    ctx.defineProperty(obj, k, v)
  return obj

proc toJSP(ctx: JSContext, parent: ref object, child: var object): JSValue =
  let p = addr child
  # Save parent as the original ancestor for this tree.
  JS_GetRuntime(ctx).getOpaque().refmap[p] = (
    (proc() =
      GC_ref(parent)),
    (proc() =
      GC_unref(parent))
  )
  let tp = getTypePtr(child)
  var needsref = false
  let val = toJSP0(ctx, p, tp, JS_UNDEFINED, needsref)
  if needsref:
    GC_ref(parent)
  return val

proc toJSP(ctx: JSContext, parent: ptr object, child: var object): JSValue =
  let p = addr child
  # Increment the reference count of parent's root ancestor, and save the
  # increment/decrement callbacks for the child as well.
  let rtOpaque = JS_GetRuntime(ctx).getOpaque()
  let ru = rtOpaque.refmap[parent]
  ru.cref()
  rtOpaque.refmap[p] = ru
  let tp = getTypePtr(child)
  return toJSP0(ctx, p, tp)