add dumpops.lua as a sample scraper that just dumps the arguments

add SkLua.h for common utilities

BUG=
R=rmistry@google.com

Review URL: https://codereview.chromium.org/15737010

git-svn-id: http://skia.googlecode.com/svn/trunk@9242 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gyp/tools.gyp b/gyp/tools.gyp
index 1fe564e..7e0a03b 100644
--- a/gyp/tools.gyp
+++ b/gyp/tools.gyp
@@ -100,6 +100,7 @@
       'sources': [
         '../tools/lua/lua_pictures.cpp',
         '../src/utils/SkLuaCanvas.cpp',
+        '../src/utils/SkLua.cpp',
       ],
       'dependencies': [
         'skia_base_libs.gyp:skia_base_libs',
diff --git a/include/utils/SkLua.h b/include/utils/SkLua.h
new file mode 100644
index 0000000..232ba9b
--- /dev/null
+++ b/include/utils/SkLua.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkLua_DEFINED
+#define SkLua_DEFINED
+
+#include "SkColor.h"
+#include "SkScalar.h"
+
+struct lua_State;
+
+class SkCanvas;
+class SkMatrix;
+class SkPaint;
+class SkPath;
+struct SkRect;
+class SkRRect;
+class SkString;
+
+#define SkScalarToLua(x)    SkScalarToDouble(x)
+#define SkLuaToScalar(x)    SkDoubleToScalar(x)
+
+class SkLua {
+public:
+    static void Load(lua_State*);
+
+    SkLua(lua_State*);
+    ~SkLua();
+
+    lua_State* getL() const { return fL; }
+
+    void pushBool(bool, const char tableKey[] = NULL);
+    void pushString(const char[], const char tableKey[] = NULL);
+    void pushString(const SkString&, const char tableKey[] = NULL);
+    void pushColor(SkColor, const char tableKey[] = NULL);
+    void pushScalar(SkScalar, const char tableKey[] = NULL);
+    void pushRect(const SkRect&, const char tableKey[] = NULL);
+    void pushRRect(const SkRRect&, const char tableKey[] = NULL);
+    void pushMatrix(const SkMatrix&, const char tableKey[] = NULL);
+    void pushPaint(const SkPaint&, const char tableKey[] = NULL);
+    void pushPath(const SkPath&, const char tableKey[] = NULL);
+    void pushCanvas(SkCanvas*, const char tableKey[] = NULL);
+
+private:
+    lua_State* fL;
+};
+
+#endif
diff --git a/src/utils/SkLua.cpp b/src/utils/SkLua.cpp
new file mode 100644
index 0000000..dff79ee
--- /dev/null
+++ b/src/utils/SkLua.cpp
@@ -0,0 +1,474 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkLua.h"
+#include "SkCanvas.h"
+#include "SkPaint.h"
+#include "SkPath.h"
+#include "SkMatrix.h"
+#include "SkRRect.h"
+#include "SkString.h"
+
+extern "C" {
+#include "lua.h"
+#include "lauxlib.h"
+}
+
+static const char gSkCanvas_MTName[] = "SkCanvas_LuaMetaTableName";
+static const char gSkMatrix_MTName[] = "SkMatrix_LuaMetaTableName";
+static const char gSkRRect_MTName[] = "SkSkRRect_LuaMetaTableName";
+static const char gSkPath_MTName[] = "SkPath_LuaMetaTableName";
+static const char gSkPaint_MTName[] = "SkPaint_LuaMetaTableName";
+
+static const char* get_mtname(const SkCanvas&) { return gSkCanvas_MTName; }
+static const char* get_mtname(const SkMatrix&) { return gSkMatrix_MTName; }
+static const char* get_mtname(const SkRRect&) { return gSkRRect_MTName; }
+static const char* get_mtname(const SkPath&) { return gSkPath_MTName; }
+static const char* get_mtname(const SkPaint&) { return gSkPaint_MTName; }
+
+template <typename T> void push_obj(lua_State* L, const T& obj) {
+    new (lua_newuserdata(L, sizeof(T))) T(obj);
+    luaL_getmetatable(L, get_mtname(obj));
+    lua_setmetatable(L, -2);
+}
+
+template <typename T> void push_ref(lua_State* L, T* ref) {
+    *(T**)lua_newuserdata(L, sizeof(T*)) = SkRef(ref);
+    luaL_getmetatable(L, get_mtname(*ref));
+    lua_setmetatable(L, -2);
+}
+
+template <typename T> T* get_ref(lua_State* L, int index) {
+    const T* ref = NULL;
+    return *(T**)luaL_checkudata(L, index, get_mtname(*ref));
+}
+
+template <typename T> T* get_obj(lua_State* L, int index) {
+    const T* obj = NULL;
+    return (T*)luaL_checkudata(L, index, get_mtname(*obj));
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+static void setfield_string(lua_State* L, const char key[], const char value[]) {
+    lua_pushstring(L, value);
+    lua_setfield(L, -2, key);
+}
+
+static void setfield_number(lua_State* L, const char key[], double value) {
+    lua_pushnumber(L, value);
+    lua_setfield(L, -2, key);
+}
+
+SkLua::SkLua(lua_State* L) : fL(L) {
+    static bool gOnce;
+    if (!gOnce) {
+        SkLua::Load(L);
+        gOnce = true;
+    }
+}
+
+SkLua::~SkLua() {
+}
+
+#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
+
+void SkLua::pushBool(bool value, const char key[]) {
+    lua_pushboolean(fL, value);
+    CHECK_SETFIELD(key);
+}
+
+void SkLua::pushString(const char str[], const char key[]) {
+    lua_pushstring(fL, str);
+    CHECK_SETFIELD(key);
+}
+
+void SkLua::pushString(const SkString& str, const char key[]) {
+    lua_pushstring(fL, str.c_str());
+    CHECK_SETFIELD(key);
+}
+
+void SkLua::pushColor(SkColor color, const char key[]) {
+    lua_newtable(fL);
+    setfield_number(fL, "a", SkColorGetA(color) / 255.0);
+    setfield_number(fL, "r", SkColorGetR(color) / 255.0);
+    setfield_number(fL, "g", SkColorGetG(color) / 255.0);
+    setfield_number(fL, "b", SkColorGetB(color) / 255.0);
+    CHECK_SETFIELD(key);
+}
+
+void SkLua::pushScalar(SkScalar value, const char key[]) {
+    lua_pushnumber(fL, SkScalarToLua(value));
+    CHECK_SETFIELD(key);
+}
+
+void SkLua::pushRect(const SkRect& r, const char key[]) {
+    lua_newtable(fL);
+    setfield_number(fL, "left", SkScalarToLua(r.fLeft));
+    setfield_number(fL, "top", SkScalarToLua(r.fTop));
+    setfield_number(fL, "right", SkScalarToLua(r.fRight));
+    setfield_number(fL, "bottom", SkScalarToLua(r.fBottom));
+    CHECK_SETFIELD(key);
+}
+
+void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
+    push_obj(fL, rr);
+    CHECK_SETFIELD(key);
+}
+
+void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
+    push_obj(fL, matrix);
+    CHECK_SETFIELD(key);
+}
+
+void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
+    push_obj(fL, paint);
+    CHECK_SETFIELD(key);
+}
+
+void SkLua::pushPath(const SkPath& path, const char key[]) {
+    push_obj(fL, path);
+    CHECK_SETFIELD(key);
+}
+
+void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
+    push_ref(fL, canvas);
+    CHECK_SETFIELD(key);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
+
+static SkScalar lua2scalar(lua_State* L, int index) {
+    SkASSERT(lua_isnumber(L, index));
+    return SkLuaToScalar(lua_tonumber(L, index));
+}
+
+static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
+    SkASSERT(lua_istable(L, index));
+    lua_pushstring(L, key);
+    lua_gettable(L, index);
+    
+    SkScalar value = lua2scalar(L, -1);
+    lua_pop(L, 1);
+    return value;
+}
+
+static U8CPU unit2byte(SkScalar x) {
+    if (x <= 0) {
+        return 0;
+    } else if (x >= 1) {
+        return 255;
+    } else {
+        return SkScalarRoundToInt(x * 255);
+    }
+}
+
+static SkColor lua2color(lua_State* L, int index) {
+    return SkColorSetARGB(unit2byte(getfield_scalar(L, index, "a")),
+                          unit2byte(getfield_scalar(L, index, "r")),
+                          unit2byte(getfield_scalar(L, index, "g")),
+                          unit2byte(getfield_scalar(L, index, "b")));
+}
+
+static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
+    rect->set(getfield_scalar(L, index, "left"),
+              getfield_scalar(L, index, "top"),
+              getfield_scalar(L, index, "right"),
+              getfield_scalar(L, index, "bottom"));
+    return rect;
+}
+
+static int lcanvas_drawColor(lua_State* L) {
+    get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
+    return 0;
+}
+
+static int lcanvas_drawRect(lua_State* L) {
+    SkRect rect;
+    get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect),
+                                      *get_obj<SkPaint>(L, 3));
+    return 0;
+}
+
+static int lcanvas_drawOval(lua_State* L) {
+    SkRect rect;
+    get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
+                                      *get_obj<SkPaint>(L, 3));
+    return 0;
+}
+
+static int lcanvas_drawCircle(lua_State* L) {
+    get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
+                                        lua2scalar(L, 3),
+                                        lua2scalar(L, 4),
+                                        *get_obj<SkPaint>(L, 5));
+    return 0;
+}
+
+static int lcanvas_getSaveCount(lua_State* L) {
+    lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
+    return 1;
+}
+
+static int lcanvas_getTotalMatrix(lua_State* L) {
+    SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
+    return 1;
+}
+
+static int lcanvas_gc(lua_State* L) {
+    get_ref<SkCanvas>(L, 1)->unref();
+    return 0;
+}
+
+static const struct luaL_Reg gSkCanvas_Methods[] = {
+    { "drawColor", lcanvas_drawColor },
+    { "drawRect", lcanvas_drawRect },
+    { "drawOval", lcanvas_drawOval },
+    { "drawCircle", lcanvas_drawCircle },
+    { "getSaveCount", lcanvas_getSaveCount },
+    { "getTotalMatrix", lcanvas_getTotalMatrix },
+    { "__gc", lcanvas_gc },
+    { NULL, NULL }
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+static int lpaint_isAntiAlias(lua_State* L) {
+    lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
+    return 1;
+}
+
+static int lpaint_setAntiAlias(lua_State* L) {
+    get_obj<SkPaint>(L, 1)->setAntiAlias(lua_toboolean(L, 2));
+    return 0;
+}
+
+static int lpaint_getColor(lua_State* L) {
+    SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
+    return 1;
+}
+
+static int lpaint_setColor(lua_State* L) {
+    get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
+    return 0;
+}
+
+static int lpaint_gc(lua_State* L) {
+    get_obj<SkPaint>(L, 1)->~SkPaint();
+    return 0;
+}
+
+static const struct luaL_Reg gSkPaint_Methods[] = {
+    { "isAntiAlias", lpaint_isAntiAlias },
+    { "setAntiAlias", lpaint_setAntiAlias },
+    { "getColor", lpaint_getColor },
+    { "setColor", lpaint_setColor },
+    { "__gc", lpaint_gc },
+    { NULL, NULL }
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+static int lpath_getBounds(lua_State* L) {
+    SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
+    return 1;
+}
+
+static int lpath_isEmpty(lua_State* L) {
+    lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
+    return 1;
+}
+
+static int lpath_isRect(lua_State* L) {
+    SkRect r;
+    bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
+    int ret_count = 1;
+    lua_pushboolean(L, pred);
+    if (pred) {
+        SkLua(L).pushRect(r);
+        ret_count += 1;
+    }
+    return ret_count;
+}
+
+static const char* dir2string(SkPath::Direction dir) {
+    static const char* gStr[] = {
+        "unknown", "cw", "ccw"
+    };
+    SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
+    return gStr[dir];
+}
+
+static int lpath_isNestedRects(lua_State* L) {
+    SkRect rects[2];
+    SkPath::Direction dirs[2];
+    bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
+    int ret_count = 1;
+    lua_pushboolean(L, pred);
+    if (pred) {
+        SkLua lua(L);
+        lua.pushRect(rects[0]);
+        lua.pushRect(rects[1]);
+        lua_pushstring(L, dir2string(dirs[0]));
+        lua_pushstring(L, dir2string(dirs[0]));
+        ret_count += 4;
+    }
+    return ret_count;
+}
+
+static int lpath_reset(lua_State* L) {
+    get_obj<SkPath>(L, 1)->reset();
+    return 0;
+}
+
+static int lpath_moveTo(lua_State* L) {
+    get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
+    return 0;
+}
+
+static int lpath_lineTo(lua_State* L) {
+    get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
+    return 0;
+}
+
+static int lpath_quadTo(lua_State* L) {
+    get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
+                                  lua2scalar(L, 4), lua2scalar(L, 5));
+    return 0;
+}
+
+static int lpath_cubicTo(lua_State* L) {
+    get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
+                                   lua2scalar(L, 4), lua2scalar(L, 5),
+                                   lua2scalar(L, 6), lua2scalar(L, 7));
+    return 0;
+}
+
+static int lpath_close(lua_State* L) {
+    get_obj<SkPath>(L, 1)->close();
+    return 0;
+}
+
+static int lpath_gc(lua_State* L) {
+    get_obj<SkPath>(L, 1)->~SkPath();
+    return 0;
+}
+
+static const struct luaL_Reg gSkPath_Methods[] = {
+    { "getBounds", lpath_getBounds },
+    { "isEmpty", lpath_isEmpty },
+    { "isRect", lpath_isRect },
+    { "isNestedRects", lpath_isNestedRects },
+    { "reset", lpath_reset },
+    { "moveTo", lpath_moveTo },
+    { "lineTo", lpath_lineTo },
+    { "quadTo", lpath_quadTo },
+    { "cubicTo", lpath_cubicTo },
+    { "close", lpath_close },
+    { "__gc", lpath_gc },
+    { NULL, NULL }
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+static const char* rrect_type(const SkRRect& rr) {
+    switch (rr.getType()) {
+        case SkRRect::kUnknown_Type: return "unknown";
+        case SkRRect::kEmpty_Type: return "empty";
+        case SkRRect::kRect_Type: return "rect";
+        case SkRRect::kOval_Type: return "oval";
+        case SkRRect::kSimple_Type: return "simple";
+        case SkRRect::kComplex_Type: return "complex";
+    }
+    SkASSERT(!"never get here");
+    return "";
+}
+
+static int lrrect_rect(lua_State* L) {
+    SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
+    return 1;
+}
+
+static int lrrect_type(lua_State* L) {
+    lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
+    return 1;
+}
+
+static int lrrect_radii(lua_State* L) {
+    int corner = lua_tointeger(L, 2);
+    SkVector v;
+    if (corner < 0 || corner > 3) {
+        SkDebugf("bad corner index %d", corner);
+        v.set(0, 0);
+    } else {
+        v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
+    }
+    lua_pushnumber(L, v.fX);
+    lua_pushnumber(L, v.fY);
+    return 2;
+}
+
+static int lrrect_gc(lua_State* L) {
+    get_obj<SkRRect>(L, 1)->~SkRRect();
+    return 0;
+}
+
+static const struct luaL_Reg gSkRRect_Methods[] = {
+    { "rect", lrrect_rect },
+    { "type", lrrect_type },
+    { "radii", lrrect_radii },
+    { "__gc", lrrect_gc },
+    { NULL, NULL }
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+class AutoCallLua {
+public:
+    AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
+        lua_getglobal(L, func);
+        if (!lua_isfunction(L, -1)) {
+            int t = lua_type(L, -1);
+            SkDebugf("--- expected function %d\n", t);
+        }
+        
+        lua_newtable(L);
+        setfield_string(L, "verb", verb);
+    }
+    
+    ~AutoCallLua() {
+        if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
+            SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
+        }
+        lua_settop(fL, -1);
+    }
+    
+private:
+    lua_State* fL;
+};
+
+#define AUTO_LUA(verb)  AutoCallLua acl(fL, fFunc.c_str(), verb)
+
+///////////////////////////////////////////////////////////////////////////////
+
+#define REG_CLASS(L, C)                             \
+    do {                                            \
+        luaL_newmetatable(L, g##C##_MTName);        \
+        lua_pushvalue(L, -1);                       \
+        lua_setfield(L, -2, "__index");             \
+        luaL_setfuncs(L, g##C##_Methods, 0);        \
+        lua_pop(L, 1); /* pop off the meta-table */ \
+    } while (0)
+
+void SkLua::Load(lua_State* L) {
+    REG_CLASS(L, SkCanvas);
+    REG_CLASS(L, SkPath);
+    REG_CLASS(L, SkPaint);
+    REG_CLASS(L, SkRRect);
+}
+
diff --git a/src/utils/SkLuaCanvas.cpp b/src/utils/SkLuaCanvas.cpp
index e13073a..be6cb66 100644
--- a/src/utils/SkLuaCanvas.cpp
+++ b/src/utils/SkLuaCanvas.cpp
@@ -6,117 +6,16 @@
  */
 
 #include "SkLuaCanvas.h"
-#include "SkRRect.h"
+#include "SkLua.h"
 
 extern "C" {
     #include "lua.h"
     #include "lauxlib.h"
 }
 
-static void setfield_string(lua_State* L, const char key[], const char value[]) {
-    lua_pushstring(L, value);
-    lua_setfield(L, -2, key);
-}
-
-static void setfield_number(lua_State* L, const char key[], double value) {
-    lua_pushnumber(L, value);
-    lua_setfield(L, -2, key);
-}
-
-static void setfield_bool(lua_State* L, const char key[], bool value) {
-    lua_pushboolean(L, value);
-    lua_setfield(L, -2, key);
-}
-
-// sets [1]...[count] in the table on the top of the stack
-static void setfield_arrayf(lua_State* L, const SkScalar array[], int count) {
-    for (int i = 0; i < count; ++i) {
-        lua_pushnumber(L, SkScalarToDouble(i + 1));     // key
-        lua_pushnumber(L, SkScalarToDouble(array[i]));  // value
-        lua_settable(L, -3);
-    }
-}
-
-static void push_rect(lua_State* L, const SkRect& r) {
-    lua_newtable(L);
-    setfield_number(L, "left", r.fLeft);
-    setfield_number(L, "top", r.fTop);
-    setfield_number(L, "right", r.fRight);
-    setfield_number(L, "bottom", r.fBottom);
-}
-
-static void setfield_rect(lua_State* L, const char key[], const SkRect& r) {
-    push_rect(L, r);
-    lua_setfield(L, -2, key);
-}
-
-static const char* rrect_type(const SkRRect& rr) {
-    switch (rr.getType()) {
-        case SkRRect::kUnknown_Type: return "unknown";
-        case SkRRect::kEmpty_Type: return "empty";
-        case SkRRect::kRect_Type: return "rect";
-        case SkRRect::kOval_Type: return "oval";
-        case SkRRect::kSimple_Type: return "simple";
-        case SkRRect::kComplex_Type: return "complex";
-    }
-    SkASSERT(!"never get here");
-    return "";
-}
-
-static void setfield_rrect(lua_State* L, const char key[], const SkRRect& rr) {
-    lua_newtable(L);
-    setfield_rect(L, "rect", rr.getBounds());
-    setfield_string(L, "type", rrect_type(rr));
-
-    SkVector rad[4] = {
-        rr.radii(SkRRect::kUpperLeft_Corner),
-        rr.radii(SkRRect::kUpperRight_Corner),
-        rr.radii(SkRRect::kLowerRight_Corner),
-        rr.radii(SkRRect::kLowerLeft_Corner),
-    };
-    setfield_arrayf(L, &rad[0].fX, 8);
-    lua_setfield(L, -2, key);
-}
-
-static void push_matrix(lua_State* L, const SkMatrix& mat) {
-    SkScalar m[9];
-    for (int i = 0; i < 9; ++i) {
-        m[i] = mat[i];
-    }
-    lua_newtable(L);
-    setfield_arrayf(L, m, 9);
-}
-
-enum PaintUsage {
-    kText_PaintUsage,
-    kImage_PaintUsage,
-    kGeometry_PaintUsage
-};
-
-static const char* color2string(SkColor c, SkString* str) {
-    str->printf("0x%08X", c);
-    return str->c_str();
-}
-
-static void setfield_paint(lua_State* L, const SkPaint& p,
-                           PaintUsage pu = kGeometry_PaintUsage) {
-    SkString str;
-
-    lua_newtable(L);
-    setfield_bool(L, "aa", p.isAntiAlias());
-    setfield_string(L, "color", color2string(p.getColor(), &str));
-
-    if (kGeometry_PaintUsage == pu) {
-        if (SkPaint::kFill_Style != p.getStyle()) {
-            setfield_number(L, "stroke-width", p.getStrokeWidth());
-        }
-    }
-    lua_setfield(L, -2, "paint");
-}
-
-class AutoCallLua {
+class AutoCallLua : public SkLua {
 public:
-    AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
+    AutoCallLua(lua_State* L, const char func[], const char verb[]) : INHERITED(L) {
         lua_getglobal(L, func);
         if (!lua_isfunction(L, -1)) {
             int t = lua_type(L, -1);
@@ -124,177 +23,27 @@
         }
 
         lua_newtable(L);
-        setfield_string(L, "verb", verb);
+        this->pushString(verb, "verb");
     }
 
     ~AutoCallLua() {
-        if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
-            SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
+        lua_State* L = this->getL();
+        if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
+            SkDebugf("lua err: %s\n", lua_tostring(L, -1));
         }
-        lua_settop(fL, -1);
+        lua_settop(L, -1);
     }
 
 private:
-    lua_State* fL;
+    typedef SkLua INHERITED;
 };
 
-#define AUTO_LUA(verb)  AutoCallLua acl(fL, fFunc.c_str(), verb)
-
-///////////////////////////////////////////////////////////////////////////////
-
-static const char gCanvasMetaTableName[] = "SkCanvas_MetaTable";
-
-static int lcanvas_getSaveCount(lua_State* L) {
-    SkCanvas* c = *(SkCanvas**)luaL_checkudata(L, 1, gCanvasMetaTableName);
-    lua_pushnumber(L, (double)c->getSaveCount());
-    return 1;
-}
-
-static int lcanvas_getTotalMatrix(lua_State* L) {
-    SkCanvas* c = *(SkCanvas**)luaL_checkudata(L, 1, gCanvasMetaTableName);
-    push_matrix(L, c->getTotalMatrix());
-    return 1;
-}
-
-static int lcanvas_gc(lua_State* L) {
-    SkCanvas** cptr = (SkCanvas**)luaL_checkudata(L, 1, gCanvasMetaTableName);
-    SkSafeUnref(*cptr);
-    *cptr = NULL;
-    return 0;
-}
-
-static const struct luaL_Reg gLuaCanvasMethods[] = {
-    { "getSaveCount", lcanvas_getSaveCount },
-    { "getTotalMatrix", lcanvas_getTotalMatrix },
-    { "__gc", lcanvas_gc },
-    { NULL, NULL }
-};
-
-static void ensure_canvas_metatable(lua_State* L) {
-    static bool gOnce;
-    if (gOnce) {
-        return;
-    }
-    gOnce = true;
-
-    luaL_newmetatable(L, gCanvasMetaTableName);
-    lua_pushvalue(L, -1);
-    lua_setfield(L, -2, "__index");
-
-    luaL_setfuncs(L, gLuaCanvasMethods, 0);
-    lua_settop(L, -2);  // pop off the meta-table
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-static const char gPathMetaTableName[] = "SkPath_MetaTable";
-
-static int lpath_getBounds(lua_State* L) {
-    SkPath* p = (SkPath*)luaL_checkudata(L, 1, gPathMetaTableName);
-    push_rect(L, p->getBounds());
-    return 1;
-}
-
-static int lpath_isEmpty(lua_State* L) {
-    SkPath* p = (SkPath*)luaL_checkudata(L, 1, gPathMetaTableName);
-    lua_pushboolean(L, p->isEmpty());
-    return 1;
-}
-
-static int lpath_isRect(lua_State* L) {
-    SkPath* p = (SkPath*)luaL_checkudata(L, 1, gPathMetaTableName);
-    SkRect r;
-    bool pred = p->isRect(&r);
-    int ret_count = 1;
-    lua_pushboolean(L, pred);
-    if (pred) {
-        push_rect(L, r);
-        ret_count += 1;
-    }
-    return ret_count;
-}
-
-static const char* dir2string(SkPath::Direction dir) {
-    static const char* gStr[] = {
-        "unknown", "cw", "ccw"
-    };
-    SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
-    return gStr[dir];
-}
-
-static int lpath_isNestedRects(lua_State* L) {
-    SkPath* p = (SkPath*)luaL_checkudata(L, 1, gPathMetaTableName);
-    SkRect rects[2];
-    SkPath::Direction dirs[2];
-    bool pred = p->isNestedRects(rects, dirs);
-    int ret_count = 1;
-    lua_pushboolean(L, pred);
-    if (pred) {
-        push_rect(L, rects[0]);
-        push_rect(L, rects[1]);
-        lua_pushstring(L, dir2string(dirs[0]));
-        lua_pushstring(L, dir2string(dirs[0]));
-        ret_count += 4;
-    }
-    return ret_count;
-}
-
-static int lpath_gc(lua_State* L) {
-    SkPath* p = (SkPath*)luaL_checkudata(L, 1, gPathMetaTableName);
-    p->~SkPath();
-    return 0;
-}
-
-static const struct luaL_Reg gLuaPathMethods[] = {
-    { "getBounds", lpath_getBounds },
-    { "isEmpty", lpath_isEmpty },
-    { "isRect", lpath_isRect },
-    { "isNestedRects", lpath_isNestedRects },
-    { "__gc", lpath_gc },
-    { NULL, NULL }
-};
-
-static void ensure_path_metatable(lua_State* L) {
-    static bool gOnce;
-    if (gOnce) {
-        return;
-    }
-    gOnce = true;
-
-    luaL_newmetatable(L, gPathMetaTableName);
-    lua_pushvalue(L, -1);
-    lua_setfield(L, -2, "__index");
-
-    luaL_setfuncs(L, gLuaPathMethods, 0);
-    lua_settop(L, -2);  // pop off the meta-table
-}
-
-static void push_path(lua_State* L, const SkPath& src) {
-    ensure_path_metatable(L);
-
-    SkPath* path = (SkPath*)lua_newuserdata(L, sizeof(SkPath));
-    new (path) SkPath(src);
-
-    luaL_getmetatable(L, gPathMetaTableName);
-    lua_setmetatable(L, -2);
-}
-
-static void setfield_path(lua_State* L, const char key[], const SkPath& path) {
-    push_path(L, path);
-    lua_setfield(L, -2, key);
-}
+#define AUTO_LUA(verb)  AutoCallLua lua(fL, fFunc.c_str(), verb)
 
 ///////////////////////////////////////////////////////////////////////////////
 
 void SkLuaCanvas::pushThis() {
-    ensure_canvas_metatable(fL);
-
-    SkCanvas** canvasPtr = (SkCanvas**)lua_newuserdata(fL, sizeof(SkCanvas*));
-    luaL_getmetatable(fL, gCanvasMetaTableName);
-    lua_setmetatable(fL, -2);
-
-    this->ref();
-    *canvasPtr = this;
+    SkLua(fL).pushCanvas(this);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -322,10 +71,10 @@
                              SaveFlags flags) {
     AUTO_LUA("saveLayer");
     if (bounds) {
-        setfield_rect(fL, "bounds", *bounds);
+        lua.pushRect(*bounds, "bounds");
     }
     if (paint) {
-        setfield_paint(fL, *paint);
+        lua.pushPaint(*paint, "paint");
     }
     return this->INHERITED::save(flags);
 }
@@ -337,29 +86,29 @@
 
 bool SkLuaCanvas::translate(SkScalar dx, SkScalar dy) {
     AUTO_LUA("translate");
-    setfield_number(fL, "dx", dx);
-    setfield_number(fL, "dy", dy);
+    lua.pushScalar(dx, "dx");
+    lua.pushScalar(dy, "dy");
     return this->INHERITED::translate(dx, dy);
 }
 
 bool SkLuaCanvas::scale(SkScalar sx, SkScalar sy) {
     AUTO_LUA("scale");
-    setfield_number(fL, "sx", sx);
-    setfield_number(fL, "sy", sy);
+    lua.pushScalar(sx, "sx");
+    lua.pushScalar(sy, "sy");
     return this->INHERITED::scale(sx, sy);
 }
 
 bool SkLuaCanvas::rotate(SkScalar degrees) {
     AUTO_LUA("rotate");
-    setfield_number(fL, "degrees", degrees);
+    lua.pushScalar(degrees, "degrees");
     return this->INHERITED::rotate(degrees);
 }
 
-bool SkLuaCanvas::skew(SkScalar sx, SkScalar sy) {
+bool SkLuaCanvas::skew(SkScalar kx, SkScalar ky) {
     AUTO_LUA("skew");
-    setfield_number(fL, "sx", sx);
-    setfield_number(fL, "sy", sy);
-    return this->INHERITED::skew(sx, sy);
+    lua.pushScalar(kx, "kx");
+    lua.pushScalar(ky, "ky");
+    return this->INHERITED::skew(kx, ky);
 }
 
 bool SkLuaCanvas::concat(const SkMatrix& matrix) {
@@ -373,22 +122,22 @@
 
 bool SkLuaCanvas::clipRect(const SkRect& r, SkRegion::Op op, bool doAA) {
     AUTO_LUA("clipRect");
-    setfield_rect(fL, "rect", r);
-    setfield_bool(fL, "aa", doAA);
+    lua.pushRect(r, "rect");
+    lua.pushBool(doAA, "aa");
     return this->INHERITED::clipRect(r, op, doAA);
 }
 
 bool SkLuaCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
     AUTO_LUA("clipRRect");
-    setfield_rrect(fL, "rrect", rrect);
-    setfield_bool(fL, "aa", doAA);
+    lua.pushRRect(rrect, "rrect");
+    lua.pushBool(doAA, "aa");
     return this->INHERITED::clipRRect(rrect, op, doAA);
 }
 
 bool SkLuaCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
     AUTO_LUA("clipPath");
-    setfield_path(fL, "path", path);
-    setfield_bool(fL, "aa", doAA);
+    lua.pushPath(path, "path");
+    lua.pushBool(doAA, "aa");
     return this->INHERITED::clipPath(path, op, doAA);
 }
 
@@ -399,44 +148,44 @@
 
 void SkLuaCanvas::drawPaint(const SkPaint& paint) {
     AUTO_LUA("drawPaint");
-    setfield_paint(fL, paint);
+    lua.pushPaint(paint, "paint");
 }
 
 void SkLuaCanvas::drawPoints(PointMode mode, size_t count,
                                const SkPoint pts[], const SkPaint& paint) {
     AUTO_LUA("drawPoints");
-    setfield_paint(fL, paint);
+    lua.pushPaint(paint, "paint");
 }
 
 void SkLuaCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
     AUTO_LUA("drawOval");
-    setfield_rect(fL, "oval", rect);
-    setfield_paint(fL, paint);
+    lua.pushRect(rect, "rect");
+    lua.pushPaint(paint, "paint");
 }
 
 void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
     AUTO_LUA("drawRect");
-    setfield_rect(fL, "rect", rect);
-    setfield_paint(fL, paint);
+    lua.pushRect(rect, "rect");
+    lua.pushPaint(paint, "paint");
 }
 
 void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
     AUTO_LUA("drawRRect");
-    setfield_rrect(fL, "rrect", rrect);
-    setfield_paint(fL, paint);
+    lua.pushRRect(rrect, "rrect");
+    lua.pushPaint(paint, "paint");
 }
 
 void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
     AUTO_LUA("drawPath");
-    setfield_path(fL, "path", path);
-    setfield_paint(fL, paint);
+    lua.pushPath(path, "path");
+    lua.pushPaint(paint, "paint");
 }
 
 void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
                                const SkPaint* paint) {
     AUTO_LUA("drawBitmap");
     if (paint) {
-        setfield_paint(fL, *paint, kImage_PaintUsage);
+        lua.pushPaint(*paint, "paint");
     }
 }
 
@@ -444,7 +193,7 @@
                                    const SkRect& dst, const SkPaint* paint) {
     AUTO_LUA("drawBitmapRectToRect");
     if (paint) {
-        setfield_paint(fL, *paint, kImage_PaintUsage);
+        lua.pushPaint(*paint, "paint");
     }
 }
 
@@ -452,7 +201,7 @@
                                      const SkPaint* paint) {
     AUTO_LUA("drawBitmapMatrix");
     if (paint) {
-        setfield_paint(fL, *paint, kImage_PaintUsage);
+        lua.pushPaint(*paint, "paint");
     }
 }
 
@@ -460,35 +209,35 @@
                                const SkPaint* paint) {
     AUTO_LUA("drawSprite");
     if (paint) {
-        setfield_paint(fL, *paint, kImage_PaintUsage);
+        lua.pushPaint(*paint, "paint");
     }
 }
 
 void SkLuaCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
                              SkScalar y, const SkPaint& paint) {
     AUTO_LUA("drawText");
-    setfield_paint(fL, paint, kText_PaintUsage);
+    lua.pushPaint(paint, "paint");
 }
 
 void SkLuaCanvas::drawPosText(const void* text, size_t byteLength,
                                 const SkPoint pos[], const SkPaint& paint) {
     AUTO_LUA("drawPosText");
-    setfield_paint(fL, paint, kText_PaintUsage);
+    lua.pushPaint(paint, "paint");
 }
 
 void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength,
                                  const SkScalar xpos[], SkScalar constY,
                                  const SkPaint& paint) {
     AUTO_LUA("drawPosTextH");
-    setfield_paint(fL, paint, kText_PaintUsage);
+    lua.pushPaint(paint, "paint");
 }
 
 void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength,
                                    const SkPath& path, const SkMatrix* matrix,
                                    const SkPaint& paint) {
     AUTO_LUA("drawTextOnPath");
-    setfield_path(fL, "path", path);
-    setfield_paint(fL, paint, kText_PaintUsage);
+    lua.pushPath(path, "path");
+    lua.pushPaint(paint, "paint");
 }
 
 void SkLuaCanvas::drawPicture(SkPicture& picture) {
@@ -503,7 +252,7 @@
                                  const uint16_t indices[], int indexCount,
                                  const SkPaint& paint) {
     AUTO_LUA("drawVertices");
-    setfield_paint(fL, paint);
+    lua.pushPaint(paint, "paint");
 }
 
 void SkLuaCanvas::drawData(const void* data, size_t length) {
diff --git a/tools/lua/dumpops.lua b/tools/lua/dumpops.lua
new file mode 100644
index 0000000..1667e57
--- /dev/null
+++ b/tools/lua/dumpops.lua
@@ -0,0 +1,34 @@
+function tostr(t)
+    local str = ""
+    for k, v in next, t do
+        if #str > 0 then
+            str = str .. ", "
+        end
+        if type(k) == "number" then
+            str = str .. "[" .. k .. "] = "
+        else
+            str = str .. tostring(k) .. " = "
+        end
+        if type(v) == "table" then
+            str = str .. "{ " .. tostr(v) .. " }"
+        else
+            str = str .. tostring(v)
+        end
+    end
+    return str
+end
+
+function sk_scrape_startcanvas(c, fileName) end
+
+function sk_scrape_endcanvas(c, fileName) end
+
+function sk_scrape_accumulate(t)
+    -- dump the params in t, specifically showing the verb first, which we
+    -- then nil out so it doesn't appear in tostr()
+    io.write(t.verb, " ")
+    t.verb = nil
+    io.write(tostr(t), "\n")
+end
+
+function sk_scrape_summarize() end
+
diff --git a/tools/lua/scrape.lua b/tools/lua/scrape.lua
index 9263612..6270188 100644
--- a/tools/lua/scrape.lua
+++ b/tools/lua/scrape.lua
@@ -1,4 +1,3 @@
--- just a helper function to dump the parameters, for debugging
 function tostr(t)
     local str = ""
     for k, v in next, t do
@@ -52,9 +51,11 @@
     local n = total[t.verb] or 0
     total[t.verb] = n + 1
 
-    if false and t.verb == "drawRect" then
-        local m = canvas:getTotalMatrix()
-        print("... ", tostr(m), "\n")
+    if false and t.verb == "drawRect" and t.paint:isAntiAlias() then
+        local r = t.rect;
+        local p = t.paint;
+        local c = p:getColor();
+        print("drawRect ", tostr(r), tostr(c), "\n")
     end
 
     if false and t.verb == "drawPath" then
@@ -67,15 +68,6 @@
                     "isRect", tostring(t.path:isRect()), tostr(t.path:getBounds()))
         end
     end
-
-    -- enable to dump all of the parameters we were sent
-    if false then
-        -- dump the params in t, specifically showing the verb first, which we
-        -- then nil out so it doesn't appear in tostr()
-        io.write(t.verb, " ")
-        t.verb = nil
-        io.write(tostr(t), "\n")
-    end
 end
 
 --[[
diff --git a/tools/lua/skia.lua b/tools/lua/skia.lua
index 88c3223..fe738d1 100644
--- a/tools/lua/skia.lua
+++ b/tools/lua/skia.lua
@@ -1,3 +1,5 @@
+-- Experimental helpers for skia --
+
 Sk = {}
 
 function Sk.isFinite(x)
@@ -78,4 +80,3 @@
 end
 
 -------------------------------------------------------------------------------
-