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
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 static int lpaint_setColor(lua_State* L) { 486 static int lpaint_setColor(lua_State* L) {
485 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2)); 487 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
486 return 0; 488 return 0;
487 } 489 }
488 490
489 static int lpaint_getTextSize(lua_State* L) { 491 static int lpaint_getTextSize(lua_State* L) {
490 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize()); 492 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSize());
491 return 1; 493 return 1;
492 } 494 }
493 495
496 static int lpaint_getTextScaleX(lua_State* L) {
497 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextScaleX());
498 return 1;
499 }
500
501 static int lpaint_getTextSkewX(lua_State* L) {
502 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getTextSkewX());
503 return 1;
504 }
505
494 static int lpaint_setTextSize(lua_State* L) { 506 static int lpaint_setTextSize(lua_State* L) {
495 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2)); 507 get_obj<SkPaint>(L, 1)->setTextSize(lua2scalar(L, 2));
496 return 0; 508 return 0;
497 } 509 }
498 510
499 static int lpaint_getTypeface(lua_State* L) { 511 static int lpaint_getTypeface(lua_State* L) {
500 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface()); 512 push_ref(L, get_obj<SkPaint>(L, 1)->getTypeface());
501 return 1; 513 return 1;
502 } 514 }
503 515
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
567 static int lpaint_getStrokeWidth(lua_State* L) { 579 static int lpaint_getStrokeWidth(lua_State* L) {
568 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth()); 580 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeWidth());
569 return 1; 581 return 1;
570 } 582 }
571 583
572 static int lpaint_setStrokeWidth(lua_State* L) { 584 static int lpaint_setStrokeWidth(lua_State* L) {
573 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2)); 585 get_obj<SkPaint>(L, 1)->setStrokeWidth(lua2scalar(L, 2));
574 return 0; 586 return 0;
575 } 587 }
576 588
589 static int lpaint_getStrokeMiter(lua_State* L) {
590 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->getStrokeMiter());
591 return 1;
592 }
593
577 static int lpaint_measureText(lua_State* L) { 594 static int lpaint_measureText(lua_State* L) {
578 if (lua_isstring(L, 2)) { 595 if (lua_isstring(L, 2)) {
579 size_t len; 596 size_t len;
580 const char* text = lua_tolstring(L, 2, &len); 597 const char* text = lua_tolstring(L, 2, &len);
581 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len)); 598 SkLua(L).pushScalar(get_obj<SkPaint>(L, 1)->measureText(text, len));
582 return 1; 599 return 1;
583 } 600 }
584 return 0; 601 return 0;
585 } 602 }
586 603
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 static int lpaint_getShader(lua_State* L) { 645 static int lpaint_getShader(lua_State* L) {
629 const SkPaint* paint = get_obj<SkPaint>(L, 1); 646 const SkPaint* paint = get_obj<SkPaint>(L, 1);
630 SkShader* shader = paint->getShader(); 647 SkShader* shader = paint->getShader();
631 if (shader) { 648 if (shader) {
632 push_ref(L, shader); 649 push_ref(L, shader);
633 return 1; 650 return 1;
634 } 651 }
635 return 0; 652 return 0;
636 } 653 }
637 654
655 static int lpaint_getAnnotation(lua_State* L) {
656 const SkPaint* paint = get_obj<SkPaint>(L, 1);
657 SkAnnotation* annotation = paint->getAnnotation();
658 if (annotation) {
659 push_ref(L, annotation);
660 return 1;
661 }
662 return 0;
663 }
664
638 static int lpaint_gc(lua_State* L) { 665 static int lpaint_gc(lua_State* L) {
639 get_obj<SkPaint>(L, 1)->~SkPaint(); 666 get_obj<SkPaint>(L, 1)->~SkPaint();
640 return 0; 667 return 0;
641 } 668 }
642 669
643 static const struct luaL_Reg gSkPaint_Methods[] = { 670 static const struct luaL_Reg gSkPaint_Methods[] = {
644 { "isAntiAlias", lpaint_isAntiAlias }, 671 { "isAntiAlias", lpaint_isAntiAlias },
645 { "setAntiAlias", lpaint_setAntiAlias }, 672 { "setAntiAlias", lpaint_setAntiAlias },
646 { "getColor", lpaint_getColor }, 673 { "getColor", lpaint_getColor },
647 { "setColor", lpaint_setColor }, 674 { "setColor", lpaint_setColor },
648 { "getTextSize", lpaint_getTextSize }, 675 { "getTextSize", lpaint_getTextSize },
649 { "setTextSize", lpaint_setTextSize }, 676 { "setTextSize", lpaint_setTextSize },
677 { "getTextScaleX", lpaint_getTextScaleX },
678 { "getTextSkewX", lpaint_getTextSkewX },
650 { "getTypeface", lpaint_getTypeface }, 679 { "getTypeface", lpaint_getTypeface },
651 { "setTypeface", lpaint_setTypeface }, 680 { "setTypeface", lpaint_setTypeface },
652 { "getFontID", lpaint_getFontID }, 681 { "getFontID", lpaint_getFontID },
653 { "getTextAlign", lpaint_getTextAlign }, 682 { "getTextAlign", lpaint_getTextAlign },
654 { "setTextAlign", lpaint_setTextAlign }, 683 { "setTextAlign", lpaint_setTextAlign },
655 { "getStroke", lpaint_getStroke }, 684 { "getStroke", lpaint_getStroke },
656 { "setStroke", lpaint_setStroke }, 685 { "setStroke", lpaint_setStroke },
657 { "getStrokeWidth", lpaint_getStrokeWidth }, 686 { "getStrokeWidth", lpaint_getStrokeWidth },
658 { "setStrokeWidth", lpaint_setStrokeWidth }, 687 { "setStrokeWidth", lpaint_setStrokeWidth },
688 { "getStrokeMiter", lpaint_getStrokeMiter },
659 { "measureText", lpaint_measureText }, 689 { "measureText", lpaint_measureText },
660 { "getFontMetrics", lpaint_getFontMetrics }, 690 { "getFontMetrics", lpaint_getFontMetrics },
661 { "getEffects", lpaint_getEffects }, 691 { "getEffects", lpaint_getEffects },
662 { "getShader", lpaint_getShader }, 692 { "getShader", lpaint_getShader },
693 { "getAnnotation", lpaint_getAnnotation },
663 { "__gc", lpaint_gc }, 694 { "__gc", lpaint_gc },
664 { NULL, NULL } 695 { NULL, NULL }
665 }; 696 };
666 697
667 /////////////////////////////////////////////////////////////////////////////// 698 ///////////////////////////////////////////////////////////////////////////////
668 699
669 static const char* mode2string(SkShader::TileMode mode) { 700 static const char* mode2string(SkShader::TileMode mode) {
670 static const char* gNames[] = { "clamp", "repeat", "mirror" }; 701 static const char* gNames[] = { "clamp", "repeat", "mirror" };
671 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames)); 702 SkASSERT((unsigned)mode < SK_ARRAY_COUNT(gNames));
672 return gNames[mode]; 703 return gNames[mode];
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
1120 REG_CLASS(L, SkShader); 1151 REG_CLASS(L, SkShader);
1121 REG_CLASS(L, SkTypeface); 1152 REG_CLASS(L, SkTypeface);
1122 REG_CLASS(L, SkMatrix); 1153 REG_CLASS(L, SkMatrix);
1123 } 1154 }
1124 1155
1125 extern "C" int luaopen_skia(lua_State* L); 1156 extern "C" int luaopen_skia(lua_State* L);
1126 extern "C" int luaopen_skia(lua_State* L) { 1157 extern "C" int luaopen_skia(lua_State* L) {
1127 SkLua::Load(L); 1158 SkLua::Load(L);
1128 return 0; 1159 return 0;
1129 } 1160 }
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