Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/utils/SkLua.cpp

Issue 109793010: Prevent crash in Lua bindings. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkLua.h" 8 #include "SkLua.h"
9 #include "SkAnnotation.h"
9 #include "SkCanvas.h" 10 #include "SkCanvas.h"
10 #include "SkData.h" 11 #include "SkData.h"
11 #include "SkDocument.h" 12 #include "SkDocument.h"
12 #include "SkImage.h" 13 #include "SkImage.h"
13 #include "SkMatrix.h" 14 #include "SkMatrix.h"
14 #include "SkPaint.h" 15 #include "SkPaint.h"
15 #include "SkPath.h" 16 #include "SkPath.h"
16 #include "SkPixelRef.h" 17 #include "SkPixelRef.h"
17 #include "SkRRect.h" 18 #include "SkRRect.h"
18 #include "SkString.h" 19 #include "SkString.h"
19 #include "SkTypeface.h" 20 #include "SkTypeface.h"
20 21
21 extern "C" { 22 extern "C" {
22 #include "lua.h" 23 #include "lua.h"
23 #include "lualib.h" 24 #include "lualib.h"
24 #include "lauxlib.h" 25 #include "lauxlib.h"
25 } 26 }
26 27
27 // return the metatable name for a given class 28 // return the metatable name for a given class
28 template <typename T> const char* get_mtname(); 29 template <typename T> const char* get_mtname();
29 #define DEF_MTNAME(T) \ 30 #define DEF_MTNAME(T) \
30 template <> const char* get_mtname<T>() { \ 31 template <> const char* get_mtname<T>() { \
31 return #T "_LuaMetaTableName"; \ 32 return #T "_LuaMetaTableName"; \
32 } 33 }
33 34
35 DEF_MTNAME(SkAnnotation)
34 DEF_MTNAME(SkCanvas) 36 DEF_MTNAME(SkCanvas)
35 DEF_MTNAME(SkDocument) 37 DEF_MTNAME(SkDocument)
36 DEF_MTNAME(SkImage) 38 DEF_MTNAME(SkImage)
37 DEF_MTNAME(SkMatrix) 39 DEF_MTNAME(SkMatrix)
38 DEF_MTNAME(SkRRect) 40 DEF_MTNAME(SkRRect)
39 DEF_MTNAME(SkPath) 41 DEF_MTNAME(SkPath)
40 DEF_MTNAME(SkPaint) 42 DEF_MTNAME(SkPaint)
41 DEF_MTNAME(SkShader) 43 DEF_MTNAME(SkShader)
42 DEF_MTNAME(SkTypeface) 44 DEF_MTNAME(SkTypeface)
43 45
44 template <typename T> T* push_new(lua_State* L) { 46 template <typename T> T* push_new(lua_State* L) {
45 T* addr = (T*)lua_newuserdata(L, sizeof(T)); 47 T* addr = (T*)lua_newuserdata(L, sizeof(T));
46 new (addr) T; 48 new (addr) T;
47 luaL_getmetatable(L, get_mtname<T>()); 49 luaL_getmetatable(L, get_mtname<T>());
48 lua_setmetatable(L, -2); 50 lua_setmetatable(L, -2);
49 return addr; 51 return addr;
50 } 52 }
51 53
52 template <typename T> void push_obj(lua_State* L, const T& obj) { 54 template <typename T> void push_obj(lua_State* L, const T& obj) {
53 new (lua_newuserdata(L, sizeof(T))) T(obj); 55 new (lua_newuserdata(L, sizeof(T))) T(obj);
54 luaL_getmetatable(L, get_mtname<T>()); 56 luaL_getmetatable(L, get_mtname<T>());
55 lua_setmetatable(L, -2); 57 lua_setmetatable(L, -2);
56 } 58 }
57 59
58 template <typename T> void push_ref(lua_State* L, T* ref) { 60 template <typename T> void push_ref(lua_State* L, T* ref) {
59 *(T**)lua_newuserdata(L, sizeof(T*)) = SkRef(ref); 61 *(T**)lua_newuserdata(L, sizeof(T*)) = SkSafeRef(ref);
60 luaL_getmetatable(L, get_mtname<T>()); 62 luaL_getmetatable(L, get_mtname<T>());
61 lua_setmetatable(L, -2); 63 lua_setmetatable(L, -2);
62 } 64 }
63 65
64 template <typename T> T* get_ref(lua_State* L, int index) { 66 template <typename T> T* get_ref(lua_State* L, int index) {
65 return *(T**)luaL_checkudata(L, index, get_mtname<T>()); 67 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
66 } 68 }
67 69
68 template <typename T> T* get_obj(lua_State* L, int index) { 70 template <typename T> T* get_obj(lua_State* L, int index) {
69 return (T*)luaL_checkudata(L, index, get_mtname<T>()); 71 return (T*)luaL_checkudata(L, index, get_mtname<T>());
(...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 static int lpaint_getShader(lua_State* L) { 630 static int lpaint_getShader(lua_State* L) {
629 const SkPaint* paint = get_obj<SkPaint>(L, 1); 631 const SkPaint* paint = get_obj<SkPaint>(L, 1);
630 SkShader* shader = paint->getShader(); 632 SkShader* shader = paint->getShader();
631 if (shader) { 633 if (shader) {
632 push_ref(L, shader); 634 push_ref(L, shader);
633 return 1; 635 return 1;
634 } 636 }
635 return 0; 637 return 0;
636 } 638 }
637 639
640 static int lpaint_getAnnotation(lua_State* L) {
641 const SkPaint* paint = get_obj<SkPaint>(L, 1);
642 SkAnnotation* annotation = paint->getAnnotation();
643 if (annotation) {
644 push_ref(L, annotation);
645 return 1;
646 }
647 return 0;
648 }
649
638 static int lpaint_gc(lua_State* L) { 650 static int lpaint_gc(lua_State* L) {
639 get_obj<SkPaint>(L, 1)->~SkPaint(); 651 get_obj<SkPaint>(L, 1)->~SkPaint();
640 return 0; 652 return 0;
641 } 653 }
642 654
643 static const struct luaL_Reg gSkPaint_Methods[] = { 655 static const struct luaL_Reg gSkPaint_Methods[] = {
644 { "isAntiAlias", lpaint_isAntiAlias }, 656 { "isAntiAlias", lpaint_isAntiAlias },
645 { "setAntiAlias", lpaint_setAntiAlias }, 657 { "setAntiAlias", lpaint_setAntiAlias },
646 { "getColor", lpaint_getColor }, 658 { "getColor", lpaint_getColor },
647 { "setColor", lpaint_setColor }, 659 { "setColor", lpaint_setColor },
648 { "getTextSize", lpaint_getTextSize }, 660 { "getTextSize", lpaint_getTextSize },
649 { "setTextSize", lpaint_setTextSize }, 661 { "setTextSize", lpaint_setTextSize },
650 { "getTypeface", lpaint_getTypeface }, 662 { "getTypeface", lpaint_getTypeface },
651 { "setTypeface", lpaint_setTypeface }, 663 { "setTypeface", lpaint_setTypeface },
652 { "getFontID", lpaint_getFontID }, 664 { "getFontID", lpaint_getFontID },
653 { "getTextAlign", lpaint_getTextAlign }, 665 { "getTextAlign", lpaint_getTextAlign },
654 { "setTextAlign", lpaint_setTextAlign }, 666 { "setTextAlign", lpaint_setTextAlign },
655 { "getStroke", lpaint_getStroke }, 667 { "getStroke", lpaint_getStroke },
656 { "setStroke", lpaint_setStroke }, 668 { "setStroke", lpaint_setStroke },
657 { "getStrokeWidth", lpaint_getStrokeWidth }, 669 { "getStrokeWidth", lpaint_getStrokeWidth },
658 { "setStrokeWidth", lpaint_setStrokeWidth }, 670 { "setStrokeWidth", lpaint_setStrokeWidth },
659 { "measureText", lpaint_measureText }, 671 { "measureText", lpaint_measureText },
660 { "getFontMetrics", lpaint_getFontMetrics }, 672 { "getFontMetrics", lpaint_getFontMetrics },
661 { "getEffects", lpaint_getEffects }, 673 { "getEffects", lpaint_getEffects },
662 { "getShader", lpaint_getShader }, 674 { "getShader", lpaint_getShader },
675 { "getAnnotation", lpaint_getAnnotation },
663 { "__gc", lpaint_gc }, 676 { "__gc", lpaint_gc },
664 { NULL, NULL } 677 { NULL, NULL }
665 }; 678 };
666 679
667 /////////////////////////////////////////////////////////////////////////////// 680 ///////////////////////////////////////////////////////////////////////////////
668 681
669 static const char* mode2string(SkShader::TileMode mode) { 682 static const char* mode2string(SkShader::TileMode mode) {
670 static const char* gNames[] = { "clamp", "repeat", "mirror" }; 683 static const char* gNames[] = { "clamp", "repeat", "mirror" };
671 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames)); 684 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
672 return gNames[mode]; 685 return gNames[mode];
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
967 static const struct luaL_Reg gSkImage_Methods[] = { 980 static const struct luaL_Reg gSkImage_Methods[] = {
968 { "width", limage_width }, 981 { "width", limage_width },
969 { "height", limage_height }, 982 { "height", limage_height },
970 { "__gc", limage_gc }, 983 { "__gc", limage_gc },
971 { NULL, NULL } 984 { NULL, NULL }
972 }; 985 };
973 986
974 /////////////////////////////////////////////////////////////////////////////// 987 ///////////////////////////////////////////////////////////////////////////////
975 988
976 static int ltypeface_gc(lua_State* L) { 989 static int ltypeface_gc(lua_State* L) {
977 get_ref<SkTypeface>(L, 1)->unref(); 990 SkSafeUnref(get_ref<SkTypeface>(L, 1));
reed1 2013/12/17 16:31:50 I believe you, but this just seems wrong. If we ha
978 return 0; 991 return 0;
979 } 992 }
980 993
981 static const struct luaL_Reg gSkTypeface_Methods[] = { 994 static const struct luaL_Reg gSkTypeface_Methods[] = {
982 { "__gc", ltypeface_gc }, 995 { "__gc", ltypeface_gc },
983 { NULL, NULL } 996 { NULL, NULL }
984 }; 997 };
985 998
986 /////////////////////////////////////////////////////////////////////////////// 999 ///////////////////////////////////////////////////////////////////////////////
987 1000
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
1120 REG_CLASS(L, SkShader); 1133 REG_CLASS(L, SkShader);
1121 REG_CLASS(L, SkTypeface); 1134 REG_CLASS(L, SkTypeface);
1122 REG_CLASS(L, SkMatrix); 1135 REG_CLASS(L, SkMatrix);
1123 } 1136 }
1124 1137
1125 extern "C" int luaopen_skia(lua_State* L); 1138 extern "C" int luaopen_skia(lua_State* L);
1126 extern "C" int luaopen_skia(lua_State* L) { 1139 extern "C" int luaopen_skia(lua_State* L) {
1127 SkLua::Load(L); 1140 SkLua::Load(L);
1128 return 0; 1141 return 0;
1129 } 1142 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698