about summary refs log blame commit diff stats
path: root/src/luasocket/compat.h
blob: 14227286f18ce8487a03b1703801b0c7303c2e4b (plain) (tree)
1
2
3


                














                                                                      
#ifndef COMPAT_H
#define COMPAT_H

#ifndef _WIN32
#pragma GCC visibility push(hidden)
#endif

void luasocket_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
void *luasocket_testudata ( lua_State *L, int arg, const char *tname);

#ifndef _WIN32
#pragma GCC visibility pop
#endif

#define luaL_setfuncs luasocket_setfuncs
#define luaL_testudata luasocket_testudata

#endif
ing-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# Return colors 'near' a given r/g/b value (expressed in hex)
# If we did this rigorously we'd need to implement cosines. So we won't.
#
# To build:
#   $ ./translate colors.mu
#
# Example session:
#   $ qemu-system-i386 code.img
#   Enter 3 hex bytes for r, g, b (lowercase; no 0x prefix) separated by a single space> aa 0 aa
#   5
# This means only color 5 in the default palette is similar to #aa00aa.

fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk) {
  var in-storage: (stream byte 0x10)
  var in/esi: (addr stream byte) <- address in-storage
  {
    # print prompt
    var x/eax: int <- draw-text-rightward screen, "Enter 3 hex bytes for r, g, b (lowercase; no 0x prefix) separated by a single space> ", 0x10/x, 0x80/xmax, 0x28/y, 3/fg/cyan, 0/bg
    # read line from keyboard
    clear-stream in
    {
      draw-cursor screen, 0x20/space
      var key/eax: byte <- read-key keyboard
      compare key, 0xa/newline
      break-if-=
      compare key, 0
      loop-if-=
      var key2/eax: int <- copy key
      append-byte in, key2
      var g/eax: grapheme <- copy key2
      draw-grapheme-at-cursor screen, g, 0xf/fg, 0/bg
      move-cursor-right 0
      loop
    }
    clear-screen screen
    # parse
    var a/ecx: int <- copy 0
    var b/edx: int <- copy 0
    var c/ebx: int <- copy 0
    # a, b, c = r, g, b
    a, b, c <- parse in
#?     set-cursor-position screen, 0x10/x, 0x1a/y
#?     draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen, a, 7/fg, 0/bg
#?     draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, " ", 7/fg, 0/bg
#?     draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen, b, 7/fg, 0/bg
#?     draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, " ", 7/fg, 0/bg
#?     draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen, c, 7/fg, 0/bg
    a, b, c <- hsl a, b, c
    # return all colors in the same quadrant in h, s and l
    print-nearby-colors screen, a, b, c
    #
    loop
  }
}

# read exactly 3 words in a single line
# Each word consists of exactly 1 or 2 hex bytes. No hex prefix.
fn parse in: (addr stream byte) -> _/ecx: int, _/edx: int, _/ebx: int {
  # read first byte of r
  var tmp/eax: byte <- read-byte in
  {
    var valid?/eax: boolean <- hex-digit? tmp
    compare valid?, 0/false
    break-if-!=
    abort "invalid byte 0 of r"
  }
  tmp <- fast-hex-digit-value tmp
  var r/ecx: int <- copy tmp
#?   set-cursor-position 0/screen, 0x10/x, 0x10/y
#?   draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, r, 7/fg, 0/bg
  # read second byte of r
  tmp <- read-byte in
  {
    {
      var valid?/eax: boolean <- hex-digit? tmp
      compare valid?, 0/false
    }
    break-if-=
    r <- shift-left 4
    tmp <- fast-hex-digit-value tmp
#?     {
#?       var foo/eax: int <- copy tmp
#?       set-cursor-position 0/screen, 0x10/x, 0x11/y
#?       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, foo, 7/fg, 0/bg
#?     }
    r <- add tmp
#?     {
#?       set-cursor-position 0/screen, 0x10/x, 0x12/y
#?       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, r, 7/fg, 0/bg
#?     }
    tmp <- read-byte in  # skip space
  }
  # read first byte of g
  var tmp/eax: byte <- read-byte in
  {
    var valid?/eax: boolean <- hex-digit? tmp
    compare valid?, 0/false
    break-if-!=
    abort "invalid byte 0 of g"
  }
  tmp <- fast-hex-digit-value tmp
  var g/edx: int <- copy tmp
#?   set-cursor-position 0/screen, 0x10/x, 0x13/y
#?   draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, g, 7/fg, 0/bg
  # read second byte of g
  tmp <- read-byte in
  {
    {
      var valid?/eax: boolean <- hex-digit? tmp
      compare valid?, 0/false
    }
    break-if-=
    g <- shift-left 4
    tmp <- fast-hex-digit-value tmp
#?     {
#?       var foo/eax: int <- copy tmp
#?       set-cursor-position 0/screen, 0x10/x, 0x14/y
#?       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, foo, 7/fg, 0/bg
#?     }
    g <- add tmp
#?     {
#?       set-cursor-position 0/screen, 0x10/x, 0x15/y
#?       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, g, 7/fg, 0/bg
#?     }
    tmp <- read-byte in  # skip space
  }
  # read first byte of b
  var tmp/eax: byte <- read-byte in
  {
    var valid?/eax: boolean <- hex-digit? tmp
    compare valid?, 0/false
    break-if-!=
    abort "invalid byte 0 of b"
  }
  tmp <- fast-hex-digit-value tmp
  var b/ebx: int <- copy tmp
#?   set-cursor-position 0/screen, 0x10/x, 0x16/y
#?   draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, b, 7/fg, 0/bg
  # read second byte of b
  {
    {
      var done?/eax: boolean <- stream-empty? in
      compare done?, 0/false
    }
    break-if-!=
    tmp <- read-byte in
    {
      var valid?/eax: boolean <- hex-digit? tmp
      compare valid?, 0/false
    }
    break-if-=
    b <- shift-left 4
    tmp <- fast-hex-digit-value tmp
#?     {
#?       var foo/eax: int <- copy tmp
#?       set-cursor-position 0/screen, 0x10/x, 0x17/y
#?       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, foo, 7/fg, 0/bg
#?     }
    b <- add tmp
#?     {
#?       set-cursor-position 0/screen, 0x10/x, 0x18/y
#?       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, b, 7/fg, 0/bg
#?     }
  }
  return r, g, b
}

# no error checking
fn fast-hex-digit-value in: byte -> _/eax: byte {
  var result/eax: byte <- copy in
  compare result, 0x39
  {
    break-if->
    result <- subtract 0x30/0
    return result
  }
  result <- subtract 0x61/a
  result <- add 0xa/10
  return result
}

fn print-nearby-colors screen: (addr screen), h: int, s: int, l: int {
#?   set-cursor-position screen, 0x10/x, 0x1c/y
#?   draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen, h, 7/fg, 0/bg
#?   draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, " ", 7/fg, 0/bg
#?   draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen, s, 7/fg, 0/bg
#?   draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, " ", 7/fg, 0/bg
#?   draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen, l, 7/fg, 0/bg
  # save just top 2 bits of each, so that we narrow down to 1/64th of the volume
  shift-right h, 6
  shift-right s, 6
  shift-right l, 6
#?   set-cursor-position screen, 0x10/x, 0x1/y
#?   draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen, h, 7/fg, 0/bg
#?   draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, " ", 7/fg, 0/bg
#?   draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen, s, 7/fg, 0/bg
#?   draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, " ", 7/fg, 0/bg
#?   draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen, l, 7/fg, 0/bg
  var a/ecx: int <- copy 0
  var b/edx: int <- copy 0
  var c/ebx: int <- copy 0
  var color/eax: int <- copy 0
  var y/esi: int <- copy 2
  {
    compare color, 0x100
    break-if->=
    a, b, c <- color-rgb color
    a, b, c <- hsl a, b, c
    a <- shift-right 6
    b <- shift-right 6
    c <- shift-right 6
    {
      compare a, h
      break-if-!=
      compare b, s
      break-if-!=
      compare c, l
      break-if-!=
      set-cursor-position screen, 0x10/x, y
      draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen screen, color, 7/fg, 0/bg
      set-cursor-position screen, 0x14/x, y
      draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, " ", 7/fg, 0/bg
      draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, "               ", 0/fg, color
#?       draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, " ", 7/fg, 0/bg
#?       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen, a, 7/fg, 0/bg
#?       draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, " ", 7/fg, 0/bg
#?       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen, b, 7/fg, 0/bg
#?       draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, " ", 7/fg, 0/bg
#?       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen, c, 7/fg, 0/bg
      y <- increment
    }
    color <- increment
    loop
  }
}