Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

Add proguard support #10

Merged
merged 4 commits into from
Jun 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
sourcemap = "0.14.0"
error-chain = "0.5.0"
proguard = "0.1.0"
error-chain = "^0.9.0"
memmap = "0.4.0"
brotli2 = "0.2.1"
varinteger = "1.0.2"
7 changes: 7 additions & 0 deletions include/libsourcemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

typedef void lsm_view_t;
typedef void lsm_index_t;
typedef void lsm_proguard_mapping_t;

typedef struct lsm_token_s {
unsigned int dst_line;
Expand Down Expand Up @@ -65,6 +66,12 @@ int lsm_view_or_index_from_json(char *bytes, unsigned int len,
lsm_index_t **idx_out,
lsm_error_t *err);

lsm_proguard_mapping_t *lsm_proguard_mapping_from_bytes(char *bytes, size_t len, lsm_error_t *err);
void lsm_proguard_mapping_free(lsm_proguard_mapping_t *view);
int lsm_proguard_mapping_has_line_info(lsm_proguard_mapping_t *view, lsm_error_t *err);
char *lsm_proguard_mapping_convert_dotted_path(
lsm_proguard_mapping_t *view, const char *path, int lineno, lsm_error_t *err);

void lsm_buffer_free(char *buf);

#endif
2 changes: 1 addition & 1 deletion libsourcemap/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .highlevel import from_json, View, Token, Index
from .highlevel import from_json, View, Token, Index, ProguardView
from .exceptions import SourceMapError, IndexedSourceMap, BadJson, \
CannotFlatten, UnsupportedMemDbVersion, BadIo, MemDbDumpError, \
TooManySources, TooManyNames, LocationOverflow, AlreadyMemDb
Expand Down
56 changes: 56 additions & 0 deletions libsourcemap/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,59 @@ def __del__(self):
self._ptr = None
except Exception:
pass


class ProguardView(object):

def __init__(self):
raise TypeError('Cannot instanciate proguard views')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*instantiate


def _get_ptr(self):
if not self._ptr:
raise RuntimeError('View is closed')
return self._ptr

@staticmethod
def from_bytes(buffer):
"""Creates a sourcemap view from a JSON string."""
buffer = to_bytes(buffer)
return ProguardView._from_ptr(rustcall(
_lib.lsm_proguard_mapping_from_bytes,
buffer, len(buffer)))

@property
def has_line_info(self):
"""Returns true if the file has line information."""
return bool(rustcall(
_lib.lsm_proguard_mapping_has_line_info, self._get_ptr()))

def lookup(self, dotted_path, lineno=None):
"""Given a dotted path in the format ``class_name`` or
``class_name:method_name`` this performs an alias lookup. For
methods the line number must be supplied or the result is
unreliaable.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*unreliable

"""
rv = None
try:
rv = rustcall(
_lib.lsm_proguard_mapping_convert_dotted_path,
self._get_ptr(),
dotted_path.encode('utf-8'), lineno or 0)
return _ffi.string(rv).decode('utf-8', 'replace')
finally:
if rv is not None:
_lib.lsm_buffer_free(rv)

@staticmethod
def _from_ptr(ptr):
rv = object.__new__(ProguardView)
rv._ptr = ptr
return rv

def __del__(self):
try:
if self._ptr:
_lib.lsm_proguard_mapping_free(self._ptr)
self._ptr = None
except Exception:
pass
61 changes: 57 additions & 4 deletions src/cabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::ffi::CStr;
use std::borrow::Cow;
use std::os::raw::{c_int, c_uint, c_char};

use proguard::MappingView;
use sourcemap::Error as SourceMapError;
use errors::{Error, ErrorKind, Result};
use unified::{View, TokenMatch, Index, ViewOrIndex};
Expand Down Expand Up @@ -42,11 +43,11 @@ pub struct CError {

fn get_error_code_from_kind(kind: &ErrorKind) -> c_int {
match *kind {
ErrorKind::SourceMapError(SourceMapError::IndexedSourcemap) => 2,
ErrorKind::SourceMapError(SourceMapError::BadJson(_, _, _)) => 3,
ErrorKind::SourceMapError(SourceMapError::CannotFlatten(_)) => 4,
ErrorKind::SourceMap(SourceMapError::IndexedSourcemap) => 2,
ErrorKind::SourceMap(SourceMapError::BadJson(_, _, _)) => 3,
ErrorKind::SourceMap(SourceMapError::CannotFlatten(_)) => 4,
ErrorKind::UnsupportedMemDbVersion => 5,
ErrorKind::IoError(_) => 6,
ErrorKind::Io(_) => 6,
ErrorKind::TooManySources => 20,
ErrorKind::TooManyNames => 21,
ErrorKind::LocationOverflow => 22,
Expand Down Expand Up @@ -272,3 +273,55 @@ export!(lsm_view_or_index_from_json(
}
}
});

export!(lsm_proguard_mapping_from_bytes(bytes: *const u8, len: c_uint)
-> Result<*mut MappingView<'static>>
{
resultbox(MappingView::from_slice(slice::from_raw_parts(bytes, len as usize))?)
});

export!(lsm_proguard_mapping_free(view: *mut MappingView) {
if !view.is_null() {
Box::from_raw(view);
}
});

export!(lsm_proguard_mapping_has_line_info(view: *const MappingView) -> Result<c_int> {
Ok(if (*view).has_line_info() {
1
} else {
0
})
});

export!(lsm_proguard_mapping_convert_dotted_path(
view: *const MappingView, path: *const c_char, lineno: c_int)
-> Result<*mut u8>
{
let path = CStr::from_ptr(path).to_str()?;
let mut iter = path.splitn(2, ':');
let cls_name = iter.next().unwrap_or("");
let meth_name = iter.next();

let s = if let Some(cls) = (*view).find_class(cls_name) {
let class_name = cls.class_name();
if let Some(meth_name) = meth_name {
let methods = cls.get_methods(meth_name, if lineno == 0 {
None
} else {
Some(lineno as u32)
});
if !methods.is_empty() {
format!("{}:{}\x00", class_name, methods[0].name())
} else {
format!("{}:{}\x00", class_name, meth_name)
}
} else {
format!("{}\x00", class_name)
}
} else {
format!("{}\x00", path)
};

Ok(Box::into_raw(s.into_boxed_str()) as *mut u8)
});
8 changes: 5 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::io;
use std::str::Utf8Error;

use proguard;
use sourcemap;


error_chain! {
foreign_links {
io::Error, IoError;
Utf8Error, Utf8Error;
sourcemap::Error, SourceMapError;
Io(io::Error);
Utf8(Utf8Error);
SourceMap(sourcemap::Error);
Proguard(proguard::Error);
}

errors {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![recursion_limit = "1024"]

extern crate sourcemap;
extern crate proguard;
extern crate memmap;
extern crate brotli2;
extern crate varinteger;
Expand All @@ -11,7 +12,7 @@ extern crate error_chain;
mod errors;
pub mod memdb;

pub use errors::{Error, ErrorKind, ChainErr, Result};
pub use errors::{Error, ErrorKind, Result};
pub use unified::{View, Index, TokenMatch};

// unified interface
Expand Down