Type Inference in Mixed Compiled / Scripting Environments

Type Inference in Mixed Compiled / Scripting Environments

Type Inference in Mixed Compiled / Scripting Environments Type Inference in Mixed Compiled / Scripting Environments Andrew Weinrich Ben Libit University of Wisconsin University of Wisconsin weinrich at cs.wisc.edu liblit at cs.wisc.edu Abstract However, the added flexibility of a scripting layer brings Programs written in statically-typed languages are com- with it several new costs. In particular, most scripting lan- monly extended with scripting engines that manipulate ob- guages lack any sort of compile-time type checking. This jects in the compiled layer. These scripting environments leads to simple but aggravating errors, such as using an in- enhance the capabilities of the program, at the cost of ad- valid method name, that would easily be caught by a type- ditional errors that would be caught by compile-time type checking compiler. In programs where most of the scripting checking. This paper describes a system for using type in- code is used in the user interface, such as web or GUI appli- formation from the compiled, statically-typed layer to per- cations, it is difficult to write unit tests for such code, and the form type inference and checking on scripting code. To presence of type-related bugs increases the time required for improve the quality of analysis, idiomatic rules based on testing and QA. common programming patterns are used to supplement the In these programs, most of the objects used in scripts are type-inference process. A proof-of-concept of this system actually implemented in code written in the compiled layer, is shown in flint, a type-checking tool for the language but all type information is thrown away when the bridge to F-Script. the scripting level is crossed. A tool that could carry this type information into the flexible scripting code and detect type errors would be of great use to programmers, not as a 1. Introduction verifier of program correctness, but as a detector of common A recent trend in software development has been to extend programming mistakes. programs written in a compiled language with a scripting Analysis of scripting code can also be improved by mak- engine that can manipulate objects from the compiled layer. ing reasonable assumptions about the coding styles and con- The presence of JavaScript in web browsers has created an ventions that programmers follow. By taking for granted that entire industry of web applications. At the OS level, many the developer follows common conventions, idiomatic rules GUI environments have object-oriented scripting languages, can be used to increase the amount of information about a such as AppleScript on Mac OS (2), that can tie together program’s structure that is available to the analyzer, allow- functionality of various programs. Scripting languages are ing for more accurate detection of common errors. also used to implement custom behavior in game engines This paper describes a generic system for performing (8), and even in mainstream commercial applications like type checking of scripting-language programs that extend Adobe’s recent product Lightroom (5). a base of code written in a different, compiled, statically- The benefits of such integrated compiled / scripting en- typed language. It also describes how convention-based rules vironments are substantial. The ability for end-users to cus- can aid the type-checking process. A proof-of-concept is tomize program behavior increases the lifespan of a product shown in flint, a type-checking and static analysis tool and fosters the creation of an ecosystem of third-party ex- for the language F-Script. F-Script is a scripting language tensions. For internal development, moving some code into derived from Smalltalk; it is unusual in that it uses the a scripting layer reduces compilation costs and turnaround compiled, statically-typed language Objective-C (1) as an time for bug fixes. implementation platform. 2. Type Checking For Mixed Compiled / Scripting Code One commonly suggested solution to the lack of compile- time type checking in scripting languages is to add explicit type declarations (4). However, proposed typing systems based on explicit declarations have encountered significant [Copyright notice will appear here once ’preprint’ option is removed.] resistance from developer communities (14), and making 1 2008/5/14 changes to the core language grammar is generally not fea- Overriding relationship for methods m and n: sible for a standalone tool. An alternative is to perform type inference. Type infer- m = hname m ! (Rm;Pm1 ;Pm2 ;:::Pmj )i ence algorithms are well-known and can be very effective, as n = hname n ! (R ;P ;P ;:::P )i demonstrated by the language ML (9). A type inference sys- n n1 n2 nk tem for the mixed environments considered in this paper has n m () name n = name m ^ the benefit of starting with a much larger set of axioms de- j = k ^ rived from the compiled code; combined with knowledge of Rn v Rm ^ common programming patterns, the type inference is much 8i : 0 ≤ i ≤ kjPni w Pmi more tractable than in the general case. Strictly speaking, not all compiled languages are stati- Subtype relationship for types A and B (treated as sets): cally typed, and not all interpreted languages lack static typ- ing. However, in this paper, we deal exclusively with en- B v A () 8mjm 2 A : 9njn 2 B ^ n m vironments in which the compiled code base is statically typed, and the interpreted script code is not. The terms “com- Type > ≡ the empty map of methods piled” and “interpreted” in this paper refer exclusively to Type ? ≡ the universal map of all methods, with ? as return statically typed and non-statically typed languages. type and > for all parameter types Table 1. Type system for object-oriented programming en- 2.1 An Object-Oriented Type System vironments. In this paper’s type system, all program entities are objects; there are no primitive values such as int. Method calls are • Classes Nearly all object-oriented languages are based the only operation on objects, and the type of an object is around explicitly named classes. Our basic type system defined solely by which methods it supports, without any does not attach names to types; however, named classes explicit naming. can be simulated by the use of appropriate methods. A A type is defined as a finite function from a set of names class can be defined as a type that contains a method (as defined by the grammar of the language under consid- that takes no arguments, returns void, and whose name eration) to ordered tuples of types. One of these name-tuple is that of the class prepended with a special token, such pairs is called a “method”. The first type in one of the tuples as “$$$”, that can never occur as part of a valid method is the method’s return value; subsequent types belong to the name in actual code. method parameters. Variadic, keyword-based, or out param- Where a method requires an instance of an exact class eters are not allowed. Instead of functions, types can also be (or subclass) as an argument, it is sufficient for the type thought of as finite sets of methods. of the parameter to contain only the special name method If we define an overriding relation for methods and a of that class. subtyping relation for types, we can define a lattice for all Many object-oriented languages also have “metaclasses,” types in a program. This system is described formally in which are objects that implements class methods. In the Table 1. In this table, types are treated as sets of methods. Java code MyJavaClass.doStaticStuff(x,y), the metaclass object would be the MyJavaClass “variable”. 2.2 Extending the Type System In Smalltalk and F-Script, the metaclass is a true object, The type system defined in Table 1 models very simple rather than simply a static namespace identifier. object-oriented languages, such as Self (13), but it is miss- In this paper, the types for a class / metaclass pair, for ing many common OO features like classes and inheritance. instance that of NSFoo, are referred to as κNSFoo and These features can be easily simulated within the type sys- κNSFoo0 . tem as follows: • Field Access Many object-oriented languages allow • Void Types This system does not have the concept of fields of objects to be directly accessed and assigned a “void” pseudotype like that of C-derived languages; without method calls. Access or assignment to a field every method must have a return value, even if there is “foo” of an object can be expressed in this paper’s pure no such semantically meaningful value. F-Script solves method-based system as the methods hgetFoo ! (x)i this problem by introducing a singleton instance of the and hsetFoo ! (κFSVoid; x)i, where x is the type of the class FSVoid, which plays a similar function as unit in field. ML: it is a placeholder that implements no methods, is • Inheritance With the representation of class types de- never required as a parameter, and is generally useless fined above, inheritance follows naturally. If ClassB is a except for solving the mapping problem. In this paper, subclass of ClassA, we define type κClassB as a method the “void” type is referred to as κFSVoid. set that contains all the methods in κClassA, including the 2 2008/5/14 special class-name method. κClassB will also contain its 2.3 Drawing Type Information from Compiled Code own class-name method and any other methods it imple- Many static type inference systems for languages that lack ments. This makes it a subtype of κClassA and provides explicit typing must operate on an entire program written in the expected subclass semantics.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    14 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us