From 00df2707e9fe88b0563d66a14e16084b40cd046b Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Fri, 11 Mar 2011 09:39:10 -0800 Subject: [PATCH] Fixed Image::error(), reports FatalException() unless .onerror is present --- src/Image.cc | 51 ++++++++++++++++++++++++++++++--------------------- src/Image.h | 8 ++++---- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/Image.cc b/src/Image.cc index a4586d6..7704820 100644 --- a/src/Image.cc +++ b/src/Image.cc @@ -189,9 +189,11 @@ Image::load() { void Image::loadSync() { - cairo_status_t status = loadSurface(); - if (status) { - error(Canvas::Error(status)); + TryCatch try_catch; + loadSurface(); + + if (try_catch.HasCaught()) { + error(try_catch); } else { loaded(); } @@ -211,7 +213,7 @@ Image::loaded() { onload->Call(Context::GetCurrent()->Global(), 0, NULL); onload.Dispose(); if (try_catch.HasCaught()) { - error(try_catch.Exception()); + error(try_catch); } } } @@ -221,10 +223,12 @@ Image::loaded() { */ void -Image::error(Local err) { +Image::error(TryCatch &try_catch) { HandleScope scope; - if (!onerror.IsEmpty()) { - Local argv[1] = { err }; + if (onerror.IsEmpty()) { + FatalException(try_catch); + } else { + Local argv[1] = { try_catch.Exception() }; onerror->Call(Context::GetCurrent()->Global(), 1, argv); onerror.Dispose(); } @@ -238,7 +242,7 @@ Image::error(Local err) { * TODO: use node IO or at least thread pool */ -cairo_status_t +Handle Image::loadSurface() { switch (extension(filename)) { case Image::PNG: return loadPNG(); @@ -246,19 +250,24 @@ Image::loadSurface() { case Image::JPEG: return loadJPEG(); #endif } - return CAIRO_STATUS_READ_ERROR; + return ThrowException(Exception::Error(String::New("failed to load image"))); } /* * Load PNG. */ -cairo_status_t +Handle Image::loadPNG() { _surface = cairo_image_surface_create_from_png(filename); width = cairo_image_surface_get_width(_surface); height = cairo_image_surface_get_height(_surface); - return cairo_surface_status(_surface); + cairo_status_t status = cairo_surface_status(_surface); + if (status) { + return ThrowException(Canvas::Error(status)); + } else { + return Undefined(); + } } #ifdef HAVE_JPEG @@ -267,17 +276,13 @@ Image::loadPNG() { * Load JPEG, convert RGB to ARGB. */ -cairo_status_t +Handle Image::loadJPEG() { FILE *stream = fopen(filename, "r"); // Generalized errors if (!stream) { - switch (errno) { - case ENOMEM: return CAIRO_STATUS_NO_MEMORY; - case ENOENT: return CAIRO_STATUS_FILE_NOT_FOUND; - default: return CAIRO_STATUS_READ_ERROR; - } + return ThrowException(ErrnoException(errno, "fopen")); } // JPEG setup @@ -294,10 +299,10 @@ Image::loadJPEG() { // Data alloc int stride = width * 4; uint8_t *data = (uint8_t *) malloc(width * height * 4); - if (!data) return CAIRO_STATUS_NO_MEMORY; + if (!data) return ThrowException(Canvas::Error(CAIRO_STATUS_NO_MEMORY)); uint8_t *src = (uint8_t *) malloc(width * 3); - if (!src) return CAIRO_STATUS_NO_MEMORY; + if (!src) return ThrowException(Canvas::Error(CAIRO_STATUS_NO_MEMORY)); // Copy RGB -> ARGB for (int y = 0; y < height; ++y) { @@ -327,8 +332,12 @@ Image::loadJPEG() { jpeg_finish_decompress(&info); jpeg_destroy_decompress(&info); cairo_status_t status = cairo_surface_status(_surface); - if (status) free(data); - return status; + if (status) { + free(data); + return ThrowException(Canvas::Error(status)); + } else { + return Undefined(); + } } #endif diff --git a/src/Image.h b/src/Image.h index 5083f72..c0c6c03 100644 --- a/src/Image.h +++ b/src/Image.h @@ -31,12 +31,12 @@ class Image: public node::ObjectWrap { inline cairo_surface_t *surface(){ return _surface; } inline uint8_t *data(){ return cairo_image_surface_get_data(_surface); } inline int stride(){ return cairo_image_surface_get_stride(_surface); } - cairo_status_t loadSurface(); - cairo_status_t loadPNG(); + Handle loadSurface(); + Handle loadPNG(); #ifdef HAVE_JPEG - cairo_status_t loadJPEG(); + Handle loadJPEG(); #endif - void error(Local); + void error(TryCatch &try_catch); void loadSync(); void loaded(); void load();